⚡️ Speed up function _annual_finder by 11%
#97
+21
−17
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.
📄 11% (0.11x) speedup for
_annual_finderinpandas/plotting/_matplotlib/converter.py⏱️ Runtime :
658 microseconds→593 microseconds(best of52runs)📝 Explanation and details
The optimized code achieves an 11% speedup through several micro-optimizations that reduce Python overhead and leverage NumPy's vectorization capabilities:
Key optimizations applied:
Direct returns in
_get_default_annual_spacing: Replaced tuple assignment with direct returns (e.g.,return (1, 1)instead of(min_spacing, maj_spacing) = (1, 1); return (min_spacing, maj_spacing)). This eliminates the intermediate variable creation and assignment overhead.Vectorized NumPy assignment: Changed from indexed assignment (
info["maj"][major_idx] = True) to direct vectorized assignment (info["maj"] = major_idx). NumPy's vectorized operations are significantly faster than element-by-element indexing.Removed redundant string assignment: Eliminated the unnecessary
info["fmt"] = ""line since NumPy's structured array initialization already pre-fills string fields with empty bytes.Direct bytes assignment: Used
b"%Y"instead of"%Y"to match the|S8dtype directly, avoiding string-to-bytes conversion overhead.Why these optimizations work:
Performance characteristics:
The optimizations show consistent 9-17% improvements across all test cases, with particularly strong performance on larger date ranges where the vectorized operations have more impact. The cache behavior remains unchanged, maintaining the same performance benefits for repeated calls.
✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_annual_finder-mhbqgejland push.