⚡️ Speed up function loop_timer by 5%
#42
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.
📄 5% (0.05x) speedup for
loop_timerinquantecon/util/timing.py⏱️ Runtime :
107 milliseconds→102 milliseconds(best of46runs)📝 Explanation and details
The optimized code achieves a 5% speedup by replacing NumPy's built-in array operations with Numba JIT-compiled helper functions for computing statistics on timing results.
Key optimizations:
JIT-compiled mean calculation: Replaced
all_times.mean()with_mean(all_times)- a simple loop-based mean calculation compiled with@njit(cache=True, fastmath=True)JIT-compiled sorting and slicing: Replaced
np.sort(all_times)[:best_of].mean()with_mean(_sort_slice(all_times, best_of))using numba-optimized functions that manually copy, sort, and slice arraysWhy this works:
cache=Trueparameter ensures compilation happens only once, making subsequent calls very fastPerformance characteristics based on test results:
test_large_scale_many_runsshows consistent gains, indicating the optimization scales wellThe core timing loop remains unchanged to preserve compatibility with arbitrary Python callables and external timing utilities, making this a safe optimization that only accelerates the post-processing of timing data.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-loop_timer-mj9qtinkand push.