Simple decorator timer in Python3.
@tick
def foo():
for i in range(100):
pass
>>> foo()
---- foo elapsed: 0.0000s ----
from functools import wraps | |
from timeit import default_timer as timer | |
def tick(f): | |
"""Simple context timer. | |
""" | |
@wraps(f) | |
def wrapper(*args, **kw): | |
start = timer() | |
res = f(*args, **kw) | |
end = timer() | |
print('---- {0} elapsed: {1:.4f}s ----'.format(f.__name__, end-start)) | |
return res | |
return wrapper |