probably better to limit the cache in size

This commit is contained in:
2024-08-05 18:22:36 +02:00
parent e036e033f9
commit 7b56e45cb6

View File

@ -5,8 +5,10 @@ import numpy as np
def npmemo(func):
"""
numpy array aware memoizer
numpy array aware memoizer with size limit
"""
maxsize = 10
order = []
cache = {}
@functools.wraps(func)
@ -15,6 +17,10 @@ def npmemo(func):
try:
return cache[key]
except KeyError:
if len(cache) >= maxsize:
oldest = order.pop(0)
cache.pop(oldest)
order.append(key)
cache[key] = res = func(*args)
return res
@ -71,6 +77,10 @@ if __name__ == "__main__":
a = np.array(a)
test(a, o)
for a, o in zip(arrays, offsets):
a = np.array(a)
test(a, o)
# print(expensive.cache)