Skip to content

Instantly share code, notes, and snippets.

@avoiney
Last active August 29, 2015 14:14
Show Gist options
  • Save avoiney/243000f5599bc17b4daa to your computer and use it in GitHub Desktop.
Save avoiney/243000f5599bc17b4daa to your computer and use it in GitHub Desktop.
""" A decorator to easily use cProfile on function """
import cProfile
def profiler(sort=-1, use_globals=False, use_locals=True):
def decr(func):
def wrp(*args, **kwargs):
g = globals() if use_globals else None
l = locals() if use_locals else None
cProfile.runctx("func(*args, **kwargs)", g, l, sort=sort)
return func(*args, **kwargs)
return wrp
return decr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment