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