sole_cache = {}
def sole(m, n, t):
if (m, n, t) in sole_cache:
return sole_cache[(m, n, t)]
else:
#do some time-consuming calculations . . .
sole_cache[(m, n, t)] = result
return result
Asked
Active
Viewed 15 times
-2
Sayse
- 41,425
- 14
- 72
- 139
Dorrin Samadian
- 1
- 2
-
To avoid doing "some time-consuming calculations" that have already been done – Sayse May 27 '22 at 11:18
-
You need to store the result somewhere - you could have used a LRU cache ... or you declare something before the function so the funciton can use it. https://docs.python.org/3/library/functools.html#functools.lru_cache - a lru_chace caches "the inputs of a function and what it returns" - if the same inputs come along it will return the last return again without recomputing. – Patrick Artner May 27 '22 at 11:19