Skip to content

Instantly share code, notes, and snippets.

@mattiaslundberg
Last active July 20, 2018 22:54
Show Gist options
  • Save mattiaslundberg/3ab3fe686ba7f3d2cd13 to your computer and use it in GitHub Desktop.
Save mattiaslundberg/3ab3fe686ba7f3d2cd13 to your computer and use it in GitHub Desktop.
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