import time
import functools
def clock(func):
@functools.wraps(func)
def clocked(*args, **kwargs):
t0 = time.time()
result = func(*args, **kwargs)
elapsed = time.time() - t0
name = func.__name__
print('[%0.8fs] %s -> %r ' % (elapsed, name, result))
return result
return clocked
@functools.lru_cache(maxsize=2)
@clock
def fibonacci(n):
if n == 0:
return 0
if n == 1:
return 1
return fibonacci(n - 1) + fibonacci(n - 2)
print(fibonacci(30))
python 测试函数运行时间装饰器
最新推荐文章于 2022-03-04 16:42:13 发布