import time def run_time(func): "计算运行时长装饰器" def wrapper(*args,**kwargs): # 包装函数 start = time.time() # 在调用装饰函数前干点事情 res = func(*args,**kwargs) end = time.time() # 在调用装饰函数后干点事情 t = end - start print(f"Total time: {t:.8f}") return res # 把装饰函数的结果返回 return wrapper @run_time def sum(num:int): sum_ = 0 for i in range(1,num+1): sum_ += i return sum_ res = sum(1000000) print(res)
标签:return,Python,res,sum,例子,time,装饰,def From: https://www.cnblogs.com/zwnsyw/p/17842629.html