Skip to content

Instantly share code, notes, and snippets.

@simonw
Forked from andrewgodwin/profile-this.py
Last active August 29, 2015 14:27
Show Gist options
  • Save simonw/51582e2e8a7c421737a2 to your computer and use it in GitHub Desktop.
Save simonw/51582e2e8a7c421737a2 to your computer and use it in GitHub Desktop.
Handy profiler pattern
import cProfile, pstats, StringIO
pr = cProfile.Profile()
pr.enable()
# ... do something ...
pr.disable()
s = StringIO.StringIO()
sortby = 'cumulative'
ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
ps.print_stats()
print s.getvalue()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment