Skip to content

Instantly share code, notes, and snippets.

@amjith
Created October 13, 2011 04:22
Show Gist options
  • Save amjith/1283366 to your computer and use it in GitHub Desktop.
Save amjith/1283366 to your computer and use it in GitHub Desktop.
Profiling decorator
def profile_func(filename=None):
def proffunc(f):
def profiled_func(*args, **kwargs):
import cProfile
import logging
logging.info('Profiling function %s' % (f.__name__))
try:
profiler = cProfile.Profile()
retval = profiler.runcall(f, *args, **kwargs)
profiler.dump_stats(filename or '%s_func.profile' % (f.__name__))
except IOError:
logging.exception(_("Could not open profile file '%(filename)s'") % {"filename": filename})
return retval
return profiled_func
return proffunc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment