import time
def stopwatch(info):
def _decorator(func):
def wrapper(*args, **kwargs):
t1 = time.time()
output = func(*args, **kwargs)
t2 = time.time()
print('%s: %.3fms' % (info,(t2-t1)*1000) )
return output
return wrapper
return _decorator
@stopwatch('sample code')
def test():
x = 0
for i in range(100000):
x += i
test()
# output
sample code: 7.291ms