Skip to content

Instantly share code, notes, and snippets.

@thetonus
Last active January 13, 2020 17:27
Show Gist options
  • Save thetonus/e86fdc13c7dc6536ada7063369321978 to your computer and use it in GitHub Desktop.
Save thetonus/e86fdc13c7dc6536ada7063369321978 to your computer and use it in GitHub Desktop.
Timing Decorator
""" Simple timing decorator """
from functools import wraps
from time import time
__all__ = ("timer")
def timer(f):
@wraps(f)
def wrap(*args, **kwargs):
start = time()
result = f(*args, **kwargs)
total = time() - start
print(f'func: {f.__name__} took: {total:2.4f} sec')
return result
return wrap
if __name__ == "__main__":
""" Test timer function """
@timer
def test():
import time
time.sleep(2.5)
test()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment