⚡️ Speed up function std by 13%
#284
Open
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.
📄 13% (0.13x) speedup for
stdinpandas/core/array_algos/masked_reductions.py⏱️ Runtime :
4.37 milliseconds→3.86 milliseconds(best of149runs)📝 Explanation and details
The optimized version introduces a fast path optimization for the most common case where there are no missing values in the data. The key changes are:
What optimization was applied:
mask_any = mask.any() if mask.size else Falsenp.std()when no values are masked, bypassing the_reductionsfunction and warning context overhead entirelyKey changes that affect behavior:
mask_anyis False (no missing values), the function directly returnsnp.std(values, axis=axis, ddof=ddof)warnings.catch_warnings()context manager is only entered when there are actually masked values that could trigger warnings_reductionsfunction is only called when necessary (when there are masked values)Why this leads to speedup:
The optimization eliminates several performance bottlenecks for the common no-missing-values case:
warnings.catch_warnings()has significant setup/teardown costs that are avoided when unnecessarynp.std()call instead of going through_reductionsfunction_reductionsPerformance characteristics based on test results:
mask.any()checkImpact on workloads:
This optimization particularly benefits data analysis pipelines where complete (non-missing) data is common, such as numerical computations on clean datasets, financial time series without gaps, or scientific measurements. The slight overhead for missing-value cases is negligible compared to the gains for complete data scenarios.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-std-mho8gfqland push.