⚡️ Speed up method Algorithms.fibonacci by 31%
#1407
Closed
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.
📄 31% (0.31x) speedup for
Algorithms.fibonacciincode_to_optimize/java/src/main/java/com/example/Algorithms.java⏱️ Runtime :
5.92 milliseconds→4.52 milliseconds(best of5runs)📝 Explanation and details
The optimized code achieves a 30% runtime improvement by replacing the exponential-time recursive Fibonacci algorithm with an iterative fast-doubling algorithm that runs in O(log n) time complexity instead of O(2^n).
Key Changes:
Algorithm replacement: The naive recursive approach makes exponential function calls (e.g., fibonacci(30) triggers over 2 million calls), while the fast-doubling method uses bit manipulation to traverse only the binary representation of n, requiring just log₂(n) iterations.
Elimination of call stack overhead: The recursive version creates deep call stacks with significant overhead for parameter passing and return value management. The iterative approach uses only local variables (a, b, c, d) with no function call overhead.
Fast-doubling technique: This algorithm leverages the mathematical property that F(2k) and F(2k+1) can be computed from F(k) and F(k+1) using simple arithmetic. By processing n's bits from most significant to least significant, it "doubles" the index at each step and conditionally adds 1, building up to F(n) efficiently.
Cache-friendly computation: Uses only primitive long operations with no object allocations, keeping all data in CPU registers for maximum performance.
Why This Optimization Matters:
The improvement is most dramatic for larger Fibonacci numbers. While both implementations handle small values (n ≤ 1) identically, the exponential growth of recursive calls makes the original version impractical for n > 40, whereas the optimized version handles even n = 90 (near the long overflow limit) in microseconds. The 30% speedup observed suggests the test cases include moderate-sized n values where the logarithmic advantage becomes significant while still being small enough that the recursive version completes in reasonable time.
✅ Correctness verification report:
⚙️ Click to see Existing Unit Tests
To edit these changes
git checkout codeflash/optimize-Algorithms.fibonacci-mlbced70and push.