Skip to content

Instantly share code, notes, and snippets.

@eruvanos
Last active December 10, 2019 23:29
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 eruvanos/27810ea0e27d88f665a472bbcbd5a61c to your computer and use it in GitHub Desktop.
Save eruvanos/27810ea0e27d88f665a472bbcbd5a61c to your computer and use it in GitHub Desktop.
Time a python funtion
# 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