Created
April 28, 2011 00:22
-
-
Save AdrienLemaire/945542 to your computer and use it in GitHub Desktop.
Performance counter 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
| 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