Example of efficiently computing Fibonacci numbers using a cache to implement a dynamic programming technique:
@
lru_cache(maxsize=None)
def fib(n):
if n < 2:
return n
return fib(n-1) + fib(n-2)
>>> [fib(n) for n in range(16)]
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610]
>>> fib.cache_info()
CacheInfo(hits=28, misses=16, maxsize=None, currsize=16)
New in version 3.2.
Changed in version 3.3: Added the typed option.
https://docs.python.org/3.3/library/functools.html#functools.lru_cache