Skip to content

Instantly share code, notes, and snippets.

@mkenne11
Last active August 29, 2015 14:23
Show Gist options
  • Save mkenne11/f6ab4e24463b0e3bed46 to your computer and use it in GitHub Desktop.
Save mkenne11/f6ab4e24463b0e3bed46 to your computer and use it in GitHub Desktop.
Function timing decorator
# 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