Skip to content

Instantly share code, notes, and snippets.

@cpdean
Created June 27, 2013 00:17
Show Gist options
  • Save cpdean/5872970 to your computer and use it in GitHub Desktop.
Save cpdean/5872970 to your computer and use it in GitHub Desktop.
simple profiling decorator with an optional comment string
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