python 计算函数执行时间装饰器(装饰器写法)

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
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 test():
sum=0
for i in range(1000):
sum+=i
print(sum)

test()

property,classmethod,staticmethod

你可能感兴趣的:(python 计算函数执行时间装饰器(装饰器写法))