← All benchmarksPython
Python lru_cache: fib(32) in 81 microseconds instead of 0.44 s
How much faster is functools.lru_cache for recursive Fibonacci in Python?
fib(32) took 0.44 s with plain recursion and 81 microseconds with @lru_cache(maxsize=None), about 5,400x faster, because each value is computed once.
Results
| plain recursion | 0.44 s |
|---|---|
| @lru_cache | 81 µs |
How it was measured
fib(32), single run each, results asserted equal.
@lru_cache(maxsize=None)
def fib(n): return n if n < 2 else fib(n-1) + fib(n-2)Why
Memoization turns exponential recursion into linear work.
When this does not apply
Only for pure functions with hashable arguments; cap maxsize when inputs vary.
Environment
MacBook Air (Apple silicon, 8 cores), macOS, Python 3.9. Measured 2026-10-12.
Source: shorts/demo-ticket/w3/lru.py in my content-engine repository. Treat any single run as a direction; re-run it on your hardware.
By Josué Barros, senior software engineer (full-stack, data and AI).