⚡️ Speed up method Fibonacci.fibonacci by 22%
#1448
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.
📄 22% (0.22x) speedup for
Fibonacci.fibonacciincode_to_optimize/java/src/main/java/com/example/Fibonacci.java⏱️ Runtime :
5.66 milliseconds→4.65 milliseconds(best of8runs)📝 Explanation and details
The optimized code achieves a 21% runtime improvement by eliminating array allocation overhead and reducing memory access costs.
Key optimization:
The original implementation allocated a
long[n+1]array to store all intermediate Fibonacci values, then accessed array elements in the loop (memo[i-1],memo[i-2]). The optimized version replaces this with just twolongvariables (prevandcurr) that track only the last two values needed for the calculation.Why this is faster:
Eliminated heap allocation: The
new long[n+1]allocation (taking ~0.25ms + ~1.39ms for initialization in line profiler) is completely removed. Array allocation requires heap memory management and initialization overhead.Reduced memory access costs: Array element access (
memo[i-1],memo[i-2]) involves bounds checking and pointer dereferencing. Direct variable access (prev,curr) is faster as these are likely register-allocated or in L1 cache. The line profiler shows the loop body reduced from 69.17ms to 53.40ms+54.79ms+55.30ms = 163.49ms total, but distributed across three simpler operations instead of complex array indexing.Better CPU cache behavior: Variables fit in registers/cache, while array access can cause cache misses, especially for larger
nvalues.Space complexity improvement: O(n) → O(1) memory usage eliminates GC pressure for large or repeated calls.
Test case performance:
The optimization benefits all test cases uniformly since the improvement comes from removing per-call overhead. Tests with larger
nvalues (liketestFibonacci_MaxSafeIndex92_ReturnsExpectedValue,testLargeInput_CompletesWithinTimeout) see proportionally larger absolute time savings due to both eliminated allocation overhead and cumulative loop efficiency gains.All functionality is preserved: error handling, edge cases (n≤1), overflow behavior, and correctness remain identical.
✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-Fibonacci.fibonacci-mlh90u7tand push.