Skip to content

Instantly share code, notes, and snippets.

@rapkin
Created December 17, 2018 23:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rapkin/97fbc977709fb387b1a7e6592c0928b3 to your computer and use it in GitHub Desktop.
Save rapkin/97fbc977709fb387b1a7e6592c0928b3 to your computer and use it in GitHub Desktop.
Decorator to measure function execution time in python
import time
def measure(method):
def timed(*args, **kw):
ts = time.time()
result = method(*args, **kw)
te = time.time()
if 'log_time' in kw:
name = kw.get('log_name', method.__name__.upper())
kw['log_time'][name] = int((te - ts) * 1000)
else:
print('%r %2.2f ms' % (method.__name__, (te - ts) * 1000))
return result
return timed
@measure
def somefn():
print(42)
somefn()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment