Memoization is an optimization technique that improves performance by storing the results of expensive function calls. When the same inputs are encountered again, the function returns the stored result instead of recalculating it. This is especially useful for recursive functions. In Python, you can easily use the functools.lru_cache decorator to memoize functions.
Example:
import functools
@functools.lru_cache(None)
def fibonacci(n):
if n <= 1:
return n
return fibonacci(n-1) + fibonacci(n-2)
This caches the result of each function call, speeding up repeated calculations by avoiding redundant work, especially in recursive functions like Fibonacci.