Last active
August 29, 2015 14:06
-
-
Save xiaohan2012/7ff1d7cb923e22cb5d66 to your computer and use it in GitHub Desktop.
Easy-to-use profiler
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
##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