Timer decorator, source code from Fluent Python
import time
import functools
def clock(func):
@functools.wraps(func)
def clocked(*args, **kwds):
t0 = time.time()
result = func(*args, **kwds)
elapsed = time.time() - t0
name = func.__name__
arg_lst = []
if args:
arg_lst.append(', '.join(repr(arg) for arg in args))
if kwds:
pairs = ['%s=%r' % (key, value) for key, value in sorted(kwds.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 hello(a, b, c, *, d, e, f):
time.sleep(1)
hello(1, 'L3nvy', False, d="D", e="E", f="F")