⚡️ Speed up method Fibonacci.fibonacci by 2,258%#2060
Closed
codeflash-ai[bot] wants to merge 1 commit intomainfrom
Closed
⚡️ Speed up method Fibonacci.fibonacci by 2,258%#2060codeflash-ai[bot] wants to merge 1 commit intomainfrom
Fibonacci.fibonacci by 2,258%#2060codeflash-ai[bot] wants to merge 1 commit intomainfrom
Conversation
The naive recursive implementation was replaced with an iterative fast-doubling algorithm that computes Fibonacci numbers in O(log n) time instead of O(2^n), reducing the function from 2.74 million calls (for n=30) to just 417 calls by eliminating exponential branching and redundant subproblem recomputation. The optimized version processes bits of n left-to-right using matrix identities F(2k) = F(k)·(2F(k+1) − F(k)) and F(2k+1) = F(k)² + F(k+1)², avoiding all recursion overhead and achieving a 23× runtime speedup (3.33 ms → 141 µs). Line profiler confirms the original spent 33% of time in recursive calls alone, now eliminated entirely with O(1) space instead of O(n) stack depth.
Collaborator
|
Closing: opened by CI workflow that shouldn't create PRs. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
📄 2,258% (22.58x) speedup for
Fibonacci.fibonacciincode_to_optimize/java/src/main/java/com/example/Fibonacci.java⏱️ Runtime :
3.33 milliseconds→141 microseconds(best of89runs)📝 Explanation and details
The naive recursive implementation was replaced with an iterative fast-doubling algorithm that computes Fibonacci numbers in O(log n) time instead of O(2^n), reducing the function from 2.74 million calls (for n=30) to just 417 calls by eliminating exponential branching and redundant subproblem recomputation. The optimized version processes bits of n left-to-right using matrix identities F(2k) = F(k)·(2F(k+1) − F(k)) and F(2k+1) = F(k)² + F(k+1)², avoiding all recursion overhead and achieving a 23× runtime speedup (3.33 ms → 141 µs). Line profiler confirms the original spent 33% of time in recursive calls alone, now eliminated entirely with O(1) space instead of O(n) stack depth.
✅ Correctness verification report:
⏪ Click to see Replay Tests
To edit these changes
git checkout codeflash/optimize-Fibonacci.fibonacci-mnt4z1l0and push.