Skip to content

Instantly share code, notes, and snippets.

@gongzhitaao
Last active December 22, 2017 22:53
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 gongzhitaao/3e1ff73576543e1f135ae86f3b622951 to your computer and use it in GitHub Desktop.
Save gongzhitaao/3e1ff73576543e1f135ae86f3b622951 to your computer and use it in GitHub Desktop.
Simple context timer in Python3

Simple decorator timer in Python3.

@tick
def foo():
  for i in range(100):
    pass
>>> foo()
---- foo elapsed: 0.0000s ----
from functools import wraps
from timeit import default_timer as timer
def tick(f):
"""Simple context timer.
"""
@wraps(f)
def wrapper(*args, **kw):
start = timer()
res = f(*args, **kw)
end = timer()
print('---- {0} elapsed: {1:.4f}s ----'.format(f.__name__, end-start))
return res
return wrapper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment