Last active
August 29, 2015 14:23
-
-
Save mkenne11/f6ab4e24463b0e3bed46 to your computer and use it in GitHub Desktop.
Function timing decorator
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
# Based on code sample at: http://www.marinamele.com/7-tips-to-time-python-scripts-and-control-memory-and-cpu-usage | |
# Decorator "fn_timer" which times a function and has argument "class-name". | |
def fn_timer(class_name): | |
def _fn_timer(function): | |
@wraps(function) | |
def function_timer(*args, **kwargs): | |
t0 = time.time() | |
result = function(*args, **kwargs) | |
t1 = time.time() | |
print ("Total time running %s:: %s: %s seconds" % | |
(class_name, function.func_name, str(t1-t0)) | |
) | |
return result | |
return function_timer | |
return _fn_timer | |
# Using the "fn_timer" decorator and passing the function class name. | |
@fn_timer("class-name") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment