Last active
December 10, 2019 23:29
-
-
Save eruvanos/27810ea0e27d88f665a472bbcbd5a61c to your computer and use it in GitHub Desktop.
Time a python funtion
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
# Timer for functions | |
from functools import wraps | |
def timeit(f): | |
@wraps(f) | |
def wrap(*args, **kwargs): | |
start = time() | |
r = f(*args, **kwargs) | |
print(f.__name__, time() - start) | |
return r | |
return wrap | |
# Timer Context Manager | |
from time import time | |
class Timer: | |
def __enter__(self): | |
self.start = time() | |
return self | |
def __exit__(self, *args, **kwargs): | |
self.end = time() - self.start | |
def time(self): | |
return self.end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment