Skip to content

Instantly share code, notes, and snippets.

@amb
Created December 30, 2018 14:18
Show Gist options
  • Save amb/ec371804f6c22c658565d1fc45f2b51b to your computer and use it in GitHub Desktop.
Save amb/ec371804f6c22c658565d1fc45f2b51b to your computer and use it in GitHub Desktop.
Simple profiling
import cProfile, pstats, io
def profiling_start():
# profiling
pr = cProfile.Profile()
pr.enable()
return pr
def profiling_end(pr):
# end profile, print results
pr.disable()
s = io.StringIO()
sortby = 'cumulative'
ps = pstats.Stats(pr, stream=s)
ps.strip_dirs().sort_stats(sortby).print_stats(20)
print(s.getvalue())
pr = profiling_start()
do_a_thing()
profiling_end(pr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment