Function Caching

Learn how to speed up Python programs using function caching with functools.lru_cache, reducing redundant computations and improving performance.

Loading...
Function Caching

Function caching is a programming optimization technique where the results of function calls are stored so that subsequent calls with the same arguments can reuse those results instead of recomputing them.

Python’s functools.lru_cache decorator provides an easy way to implement function caching.

Using functools.lru_cache

The lru_cache decorator caches the most recent function calls up to a specified maxsize.

If maxsize=None, the cache can grow indefinitely.

Example: Fibonacci Sequence

# Without Caching:
 
def fib(n):
    if n < 2:
        return n
    return fib(n - 1) + fib(n - 2)
 
print(fib(20))  # Very slow
 
# With Caching:
 
import functools
 
@functools.lru_cache(maxsize=None)
def fib(n):
    if n < 2:
        return n
    return fib(n - 1) + fib(n - 2)
 
print(fib(20))  # Much faster

Key Details:

  1. @functools.lru_cache: Automatically caches the function results.
  2. Recursive Efficiency: Without caching, the fib function would perform redundant calculations, resulting in exponential time complexity. Caching reduces this to linear complexity.

Benefits of Function Caching

  1. Performance Improvement: Avoids redundant computations.
  2. Ease of Use: With lru_cache, caching logic is handled automatically.
  3. Memory Management: Old or least-used entries are removed when the cache reaches its maxsize.

Cache Configuration

  • maxsize: Maximum number of cached results. Default is 128. Use None for unlimited cache.
  • typed: If True, caches are separate for functions with arguments of different types.
@functools.lru_cache(maxsize=100, typed=True)
def example(x):
    return x * 2

Inspecting and Managing the Cache

  • cache_info(): Returns statistics about the cache (hits, misses, current size, max size).
  • cache_clear(): Clears the cache.
fib.cache_info()  # Output: CacheInfo(hits=19, misses=21, maxsize=None, currsize=21)
fib.cache_clear()

Best Practices for Function Caching

  • Use caching for functions with expensive computations (e.g., recursion, heavy math, API calls).
  • Avoid caching functions with side effects (e.g., database writes, printing, random number generation).
  • Clear cache (.cache_clear()) when results may change due to external factors (e.g., updated data).
  • Be mindful of memory if using maxsize=None.

👉 Next tutorial: Handling Files in Python

Support my work!