Skip to content

Instantly share code, notes, and snippets.

@xiaohan2012
Last active August 29, 2015 14:06
Show Gist options
  • Save xiaohan2012/7ff1d7cb923e22cb5d66 to your computer and use it in GitHub Desktop.
Save xiaohan2012/7ff1d7cb923e22cb5d66 to your computer and use it in GitHub Desktop.
Easy-to-use profiler
##Usage:
# with profiler_manager():
# do want you want
from contextlib import contextmanager
import cProfile, pstats, StringIO
@contextmanager
def profiler_manager():
pr = cProfile.Profile()
pr.enable()
yield
pr.disable()
s = StringIO.StringIO()
sortby = 'cumulative'
s = pstats.Stats(pr, stream=s).sort_stats(sortby)
s.print_stats()
print s.getvalue()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment