Created
July 10, 2019 13:37
-
-
Save andybell/27aea4a5742a29242e948fe91710a8b8 to your computer and use it in GitHub Desktop.
Timer decorator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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