Skip to content

Instantly share code, notes, and snippets.

@andybell
Created July 10, 2019 13:37
Show Gist options
  • Save andybell/27aea4a5742a29242e948fe91710a8b8 to your computer and use it in GitHub Desktop.
Save andybell/27aea4a5742a29242e948fe91710a8b8 to your computer and use it in GitHub Desktop.
Timer decorator
import time
# timer decorator
# adapted from https://medium.com/pythonhive/python-decorator-to-measure-the-execution-time-of-methods-fa04cb6bb36d
def timeit(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))
else:
print('{} {:.2f} seconds'.format(method.__name__, (te - ts)))
return result
return timed
@timeit
def demoFunction():
time.sleep(2)
return
demoFunction()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment