使用装饰器查看函数运行时间
from functools import wraps import time def timefn(fn): """计算性能的修饰器""" @wraps(fn) def measure_time(*args, **kwargs): t1 = time.perf_counter() result = fn(*args, **kwargs) t2 = time.perf_counter() print(f"{fn.__name__}:{t2 - t1:.2f}s") return result return measure_time # 在定义函数前进行装饰 @timefn def test(): s = 0 for i in range(1000000): s += 1 return s test()
https://blog.csdn.net/ZeropointS/article/details/88815645
本文来自博客园,作者:CodeYaSuo,转载请注明原文链接:https://www.cnblogs.com/hany-postq473111315/p/16359114.html