Skip to content

Instantly share code, notes, and snippets.

@toddbirchard
Last active August 26, 2023 00:56
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 toddbirchard/74bc8eb0415d3a0d9a51c39d4dc3e94f to your computer and use it in GitHub Desktop.
Save toddbirchard/74bc8eb0415d3a0d9a51c39d4dc3e94f to your computer and use it in GitHub Desktop.
Python Decorator to determine execution time of a given function
"""Decorator to determine execution time of a given function."""
from time import time
def timeit(method: func):
"""Print execution time of decorated function."""
def timed(*args, **kw):
ts = time()
result = method(*args, **kw)
te = time()
if 'log_time' in kw:
name = kw.get('log_name', method.__name__.upper())
kw['log_time'][name] = int((te - ts) * 1000)
else:
print(f"%r %2.2f ms" % \
(method.__name__, (te - ts) * 1000))
return result
return timed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment