store the results of expensive function calls and return the cached result when the same inputs occur again
- decorator to optimize performance-intensive functions
- Limited and Predictable Input Variance
- Repetitive Computations with Identical Inputs
from functools import partial, lru_cache
import numpy as np
@lru_cache(maxsize=128)
def expensive_computation(x, y):
return np.sum(np.power(np.arange(x), y))
partial_compute = partial(
expensive_computation, y=2)
result1 = partial_compute(1000000)
result2 = partial_compute(1000000)
print(f"Result 1: {result1}")
print(f"Result 2: {result2}")