Last active
May 26, 2016 19:34
-
-
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
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
# 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