跳到主要内容

计时器

用于计算函数运行时间的装饰器

timer
from functools import wraps
from time import perf_counter

def timer(func):
'''
Decorator that reports the execution time.
'''

@wraps(func)
def wrapper(*args, **kwargs):
start = perf_counter()
result = func(*args, **kwargs)
end = perf_counter()
print(f"FUNCTION: {func.__name__}\ncost time: {end-start:.4f} seconds")
return result

return wrapper
请作者喝可乐🥤:
本文遵循 CC 4.0 BY-SA站外链接 版权协议,转载请标明出处