Skip to content

Instantly share code, notes, and snippets.

@AdrienLemaire
Created April 28, 2011 00:22
Show Gist options
  • Select an option

  • Save AdrienLemaire/945542 to your computer and use it in GitHub Desktop.

Select an option

Save AdrienLemaire/945542 to your computer and use it in GitHub Desktop.
Performance counter decorator
def time_and_log(func):
"""
A decorator to call a function and log the time spent inside
Usage:
@time_and_log
somefunc(someargs):
pass
"""
@functools.wraps(func)
def wrapper(*args, **kargs):
# XXX not monotonic time, needs C binding to clock_monotonic
tstart = time.time()
try:
return func(*args, **kargs)
finally:
tend = time.time()
LOGGER.info('%s took %f seconds', func.__name__, tend - tstart)
return wrapper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment