Created
July 17, 2018 10:39
-
-
Save packmad/11f760c57f2d68640178e4400cab2015 to your computer and use it in GitHub Desktop.
Decorator for function elapsed time
This file contains hidden or 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
import time | |
import timeit | |
def time_usage(func): | |
def wrapper(*args, **kwargs): | |
start_time = timeit.default_timer() | |
retval = func(*args, **kwargs) | |
elapsed = timeit.default_timer() - start_time | |
print "Function '{}' elapsed time: {}sec".format(func.__name__, elapsed) | |
return retval | |
return wrapper | |
@time_usage | |
def test(): | |
for i in xrange(0, 10000): | |
pass | |
time.sleep(2) | |
if __name__ == "__main__": | |
test() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment