⚡️ Speed up function leapfrog_integration by 32,859%
#1071
+2
−0
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.
📄 32,859% (328.59x) speedup for
leapfrog_integrationincode_to_optimize/sample_code.py⏱️ Runtime :
956 milliseconds→2.90 milliseconds(best of101runs)📝 Explanation and details
The optimized code achieves a 329x speedup (32859%) by adding a single critical change: the
@numba.njit(cache=True)decorator.What Changed
@numba.njit(cache=True)compiles the function to native machine codeimport numbaat the topWhy This Optimization Works
Numba eliminates Python interpreter overhead - The original code spends most of its time in nested loops performing simple arithmetic operations. Line profiler shows the inner loops (particle-particle interactions) account for ~85% of runtime with intensive array indexing and floating-point operations. Python's interpreter adds significant overhead for:
pos[j, 0],acc[i, 1], etc.)JIT compilation converts Python to optimized machine code - Numba translates the function to LLVM intermediate representation, then to native code that runs at C/C++ speeds. For this computation-heavy, loop-intensive code with minimal Python object overhead, JIT compilation provides near-optimal performance.
Cache=True avoids recompilation - The compiled function is cached to disk, so subsequent runs skip compilation overhead entirely.
Test Results Patterns
The optimization shows dramatic improvements for compute-intensive scenarios:
The optimization is particularly effective for the hot path: the triply-nested loop computing pairwise gravitational forces, which dominates runtime in realistic N-body simulations.
Impact Considerations
This is a drop-in optimization with minimal risk:
cache=Trueflag ensures the first-run compilation cost is amortized across executions✅ Correctness verification report:
⚙️ Click to see Existing Unit Tests
test_numba_jit_code.py::TestLeapfrogIntegration.test_does_not_modify_inputtest_numba_jit_code.py::TestLeapfrogIntegration.test_momentum_conservationtest_numba_jit_code.py::TestLeapfrogIntegration.test_single_moving_particletest_numba_jit_code.py::TestLeapfrogIntegration.test_single_stationary_particletest_numba_jit_code.py::TestLeapfrogIntegration.test_two_particles_approach🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-leapfrog_integration-mkgfarwhand push.