import time
from functools import lru_cache
@lru_cache(maxsize=128) # 指定128个不同参数组合的调用结果,lru策略,设置为None表示无上限
def test(step: int, turns: int) -> int:
total = 0
for _ in range(turns):
total += step
return total
# 打印初始的缓存信息
print(test.cache_info())
# 测试缓存创建前函数耗时
start = time.time()
test(1, 1000 * 1000 * 30)
end = time.time()
print(end - start)
print(test.cache_info())
# 测试缓存创建后函数耗时
start = time.time()
test(1, 1000 * 1000 * 30)
end = time.time()
print(end - start)
print(test.cache_info())
# 清理缓存
test.cache_clear()
# 测试清理缓存后函数耗时
start = time.time()
test(1, 1000 * 1000 * 30)
end = time.time()
print(end - start)