⚡️ Speed up method IVP.solve by 34%
#61
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.
📄 34% (0.34x) speedup for
IVP.solveinquantecon/_ivp.py⏱️ Runtime :
26.6 milliseconds→19.9 milliseconds(best of250runs)📝 Explanation and details
The optimized code achieves a 33% speedup primarily through eliminating repeated array allocations in the integration trajectory building process.
Key Optimizations Applied:
Numba-accelerated array operations: Added
@njitcompiled functions_hstack_numba()and_vstack_numba()that replace the expensivenp.hstack()andnp.vstack()calls. These eliminate Python function call overhead and optimize memory access patterns.Batch array construction strategy: Instead of repeatedly calling
np.vstack()to grow the solution array at each integration step (which creates new arrays each time), the optimized version:np.vstack()operation at the endWhy This Leads to Speedup:
The original profiler results show that
np.hstack()andnp.vstack()operations consumed ~52% of total runtime (26.3% + 25.7% in_integrate_fixed_trajectory). The optimized version reduces this overhead significantly by:Performance Impact by Test Cases:
The optimization is most effective for:
test_large_number_of_stepsandtest_large_backward_integrationThe optimization maintains identical behavior and accuracy while providing consistent performance gains across all integration scenarios, with larger benefits for computationally intensive cases involving many integration steps.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-IVP.solve-mja75vkdand push.