⚡️ Speed up function _quarterly_finder by 10%
#96
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
📄 10% (0.10x) speedup for
_quarterly_finderinpandas/plotting/_matplotlib/converter.py⏱️ Runtime :
970 microseconds→885 microseconds(best of9runs)📝 Explanation and details
The optimization achieves a 9% speedup through several key improvements:
1. Direct Return Statements
_get_default_annual_spacing()(e.g.,return (1, 2)instead of(min_spacing, maj_spacing) = (1, 2); return (min_spacing, maj_spacing))2. Streamlined Conditional Logic
has_level_label(), extractedsize = label_flags.sizeto avoid repeated attribute accesselsebranch3. Optimized Array Operations in
_quarterly_finder()b"Q%q") instead of strings for numpy structured array assignments, avoiding string-to-bytes conversion overhead for the"|S8"dtypeinfo["fmt"] = ""assignment since zero-initialized structured arrays already have empty string fieldsvmin_i,vmax_i) and intermediate variables for better code clarity4. Reduced Redundant ppd Initialization
ppd = -1assignments closer to where they're needed instead of initializing once at the top, though this has minimal performance impactThese optimizations are particularly effective for:
The improvements focus on micro-optimizations that reduce Python object creation and unnecessary operations while maintaining identical functionality.
✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
plotting/test_converter.py::test_quarterly_finderTo edit these changes
git checkout codeflash/optimize-_quarterly_finder-mhbq5qtuand push.