python functool.lru_cache

Creator
Creator
Seonglae Cho
Created
Created
2024 Jan 12 1:52
Editor
Edited
Edited
2024 Oct 18 23:4
Refs
Refs

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) # Usage result1 = partial_compute(1000000) result2 = partial_compute(1000000) #Cached result print(f"Result 1: {result1}") print(f"Result 2: {result2}") # Result 1: 333332833333500000 # Result 2: 333332833333500000
 
 
 
 
 
 
 

Recommendations