This small script benchmarks four methods to compute Fibonacci numbers and plots the timings:
- naive recursive
- recursive with memoization (top-down DP)
- bottom-up iterative (array-like)
- matrix exponentiation (O(log n))
Run the script with n values you want to test:
python3 fib_bench.py 5 10 20 30 35 --output fib_times.png- The naive recursive method is exponential and will become infeasible quickly. By default the script skips running naive recursion for n > 35; use
--allow-slowto force running it for larger n (not recommended). - The script saves a PNG plot (default
fib_bench.png) showing timing lines for each method.