def metric(fn): @functools.wraps(fn) def wrapper(*args,**kw): start = time.perf_counter() t = fn(*args,**kw) end = time.perf_counter() print('%s executed in %s ms' % (fn.__name__, (end - start))) return t return wrapper
结果: fast executed in 0.001296294560962212 ms slow executed in 0.12408149386680874 ms
Sign in to make a reply
Junhong1994
def metric(fn): @functools.wraps(fn) def wrapper(*args,**kw): start = time.perf_counter() t = fn(*args,**kw) end = time.perf_counter()
print('%s executed in %s ms' % (fn.__name__, (end - start))) return t return wrapper
结果: fast executed in 0.001296294560962212 ms slow executed in 0.12408149386680874 ms