Skip to content

Instantly share code, notes, and snippets.

@x10an14
Last active May 26, 2016 19:34
Show Gist options
  • Save x10an14/98090cca7cbe240f3c9eb0d02572c722 to your computer and use it in GitHub Desktop.
Save x10an14/98090cca7cbe240f3c9eb0d02572c722 to your computer and use it in GitHub Desktop.
A Python function wrapper which in Python 3.3+ uses the most accurate timing measurements
# Original honor: http://stackoverflow.com/a/15136358/1503549
import time
def timeit(f):
def decorated_function(*args, **kw):
# Time execution
starttime = time.perf_counter()
result = f(*args, **kw)
endtime = time.perf_counter()
# Report result
print('func:{}():\n\targs={}\n\tkwargs={}\n\t'
'took: {:.4f} sec'.format(f.__name__, args, kw, endtime - starttime))
return result
return decorated_function
# Usage 1:
@timeit
def foo():
time.sleep(2)
return
# Usage 2:
foo = timeit(foo)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment