Created
June 27, 2013 00:17
-
-
Save cpdean/5872970 to your computer and use it in GitHub Desktop.
simple profiling decorator with an optional comment string
This file contains 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
import functools | |
import logging | |
import time | |
logging.basicConfig(level=logging.INFO) | |
def profile(comment=""): | |
def time_part(f): | |
@functools.wraps(f) | |
def w(*args, **kwargs): | |
start = time.time() | |
result = f(*args, **kwargs) | |
dur = time.time() - start | |
name = f.__name__ | |
logging.info(" {2} {0} : {1}".format(name, dur, comment)) | |
return result | |
return w | |
return time_part | |
@profile("this is a one") | |
def one(): | |
time.sleep(1) | |
return 1 | |
@profile("this is a two") | |
def two(): | |
time.sleep(2) | |
return 2 | |
one() | |
two() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment