I have a python code snippet that allows me to time function as a decorator. I would like to add function name to the output. and time in milli-seconds
def func_timer(func):
def f(*args, **kwargs):
start = time.time()
results = func(*args, **kwargs)
print "Elapsed: %.6fs" % (time.time() - start)
return results
return f
Usage is:
@func_timer
def foo():
pass
Current Output is :
Elapsed: 0.005168s
Output desired:
foo Elapsed: 5.168ms
Function objects have a __name__
attribute, you can use that. Simply multiply the time by 1000 if you want milliseconds:
print "%s Elapsed: %.6fms" % (func.__name__, (time.time() - start) * 1000)