Pro Python: making code faster
Before optimising, measure (with time.perf_counter() or a profiler) to find what is really slow. The problem is often a calculation redone thousands of times, or a search in a list (slow) instead of a set or dictionary (instant).
Memoisation keeps already-computed results in memory. The @functools.lru_cache decorator does it in one line: on the second call with the same arguments, the function returns the result straight away. A recursive function that made millions of calls now makes only a few dozen.
import time
from functools import lru_cache
def slow_fib(n):
return n if n < 2 else slow_fib(n - 1) + slow_fib(n - 2)
@lru_cache(maxsize=None)
def fast_fib(n):
return n if n < 2 else fast_fib(n - 1) + fast_fib(n - 2)
for f in (slow_fib, fast_fib):
start = time.perf_counter()
f(25)
print(f.__name__, "took", "a while" if time.perf_counter() - start > 0.01 else "almost nothing")