python clock装饰器 计算函数执行时间,执行结果及传入的参数
import time import functools def clock(func): @functools.wraps(func)#还原被装饰函数的__name__和__doc__属性 def clocked(*args,**kwargs):#支持关键字参数 t0 = time.perf_counter() result = func(*args,**kwargs) elapsed = time.perf_counter()- t0 name = func.__name__ arg_lst = [] if args: arg_lst.append(','.join(repr(arg) for arg in args)) if kwargs: pairs = ['%s = %r' % (k,w) for k,w in sorted(kwargs.items())] arg_lst.append(','.join(pairs)) arg_str = ','.join(arg_lst) print('[%0.8fs]%s(%s) -> %r' % (elapsed,name,arg_str,result)) return result return clocked @clock def snooze(seconds): time.sleep(seconds) @clock def factorial(n): return 1 if n<2 else n * factorial(n-1) if __name__ == '__main__': print('*'*40,'Calling snooze(.123)') snooze(.123) print('*'*40,'Calling factorial(6)') print('6! =',factorial(6))