Skip to content

Instantly share code, notes, and snippets.

@bonfy
Created February 12, 2018 02:23
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 bonfy/33f1f62d1476b713e4c71bbcbff498c0 to your computer and use it in GitHub Desktop.
Save bonfy/33f1f62d1476b713e4c71bbcbff498c0 to your computer and use it in GitHub Desktop.
Timer
# 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