Ben*_*Ben 6 python memoization
我的理解是 functools @cache 装饰器将记住参数。然而,对于类的实例方法,参数包括“self”。这会对性能产生影响吗?
class Test:
def __init__(self):
self.cache = {}
def test(self, a, b):
result = self.cache.get((a,b))
if result is None:
result = a + b
self.cache[(a,b)] = result
return result
Run Code Online (Sandbox Code Playgroud)
上面的代码会比简单地用 @functools.cache 装饰 test() 表现更差吗?