Created
February 12, 2018 02:23
-
-
Save bonfy/33f1f62d1476b713e4c71bbcbff498c0 to your computer and use it in GitHub Desktop.
Timer
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
# timer.py | |
import time | |
# Decorator function that prints the time that a function takes to run. | |
def timedFunction(func): | |
# The wrapper function will take whatever arguments the original does. | |
def wrapper(*args, **kwargs): | |
# Call the function we're decorating, recording its start and end time. | |
start = time.time() | |
value = func(*args, **kwargs) | |
end = time.time() | |
# Calculate and print the elapsed time of the decorated function. | |
elapsed = end - start | |
print "%s took %2.4f seconds to run." % (func.__name__, elapsed) | |
# Return the decorated function's value. | |
return value | |
return wrapper |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment