⚡️ Speed up method Algorithms.fibonacci by 42%
#1409
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.
📄 42% (0.42x) speedup for
Algorithms.fibonacciincode_to_optimize/java/src/main/java/com/example/Algorithms.java⏱️ Runtime :
7.17 milliseconds→5.04 milliseconds(best of5runs)📝 Explanation and details
The optimized code achieves a 42% runtime improvement (from 7.17ms to 5.04ms) by eliminating redundant bit shift operations in the fast-doubling loop.
Key Optimization:
The original loop performs two bit operations per iteration:
(n >> i)- shifts n right by i bits& 1- masks the least significant bitSince
idecrements fromhighestBitto 0, this effectively scans bits from most significant to least significant, but does so by repeatedly shifting the entire valuen.The optimized version precomputes a bit mask (
bit = 1 << highestBit) and shifts the mask itself down each iteration (bit >>= 1). The check becomes(n & bit) == 0, which tests whether a specific bit innis set without shiftingnat all.Why This Is Faster:
Fewer shift operations: The original code shifts
nby varying amounts on every loop iteration (O(log n) shifts of potentially large values). The optimized code performs a single initial shift to create the mask, then shifts only the small mask value once per iteration.Simpler bit extraction: Masking against a precomputed bit position (
n & bit) is slightly more efficient than the shift-then-mask pattern ((n >> i) & 1), as it avoids the variable-shift operation entirely.Reduced instruction count: Each iteration saves one variable-shift operation and replaces it with a fixed right-shift of a small constant, which modern CPUs can execute more efficiently.
For the fast-doubling algorithm (used when n ≥ 50), this optimization reduces the per-iteration overhead in what is already a logarithmic-time algorithm. The 42% speedup demonstrates that even small micro-optimizations in tight loops can compound significantly when computing large Fibonacci numbers where the loop runs log₂(n) times.
✅ Correctness verification report:
⚙️ Click to see Existing Unit Tests
To edit these changes
git checkout codeflash/optimize-Algorithms.fibonacci-mlbdfzsnand push.