Skip to content

Instantly share code, notes, and snippets.

@mattiaslundberg
Last active Jul 20, 2018
Embed
What would you like to do?
Inline profiling in Python (2.7)
# https://docs.python.org/2/library/profile.html
import cProfile
pr = cProfile.Profile()
pr.enable()
try:
pass # Code to profile
finally:
pr.disable()
pr.dump_stats('filename')
import pstats
s = pstats.Stats('filename')
# print calls taking most time
s.sort_stats('tottime').print_stats('appname', .05)
# print most called calls
s.sort_stats('ncalls').print_stats('appname', .05)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment