User Tools

Site Tools


python3:lru_cache

This is an old revision of the document!


LRU Cache

Memoization is a common optimization technique where repeatedly computed values are cached for quick lookup. While memoization can be achieved by a relatively simple modification to many recursive formulations, Python3's functools library implements automatic memoization in the form of an LRU (least recently used) cache.

The Fibonacci Sequence

The Fibonacci sequence (fn = fn-1+fn-2) is a canonical example of a naive recursive function with exponential complexity.

fibonacci.py
def fib(n):
    if n < 2:
        return 1
    return fib(n-1) + fib(n-2)
 
print(fib(40))
python3/lru_cache.1541280618.txt.gz · Last modified: 2018/11/03 16:30 by jguerin