Skip to content

Instantly share code, notes, and snippets.

@mezhaka
Created August 28, 2018 06:23
Show Gist options
  • Save mezhaka/6198245f36762e5d332a746e8454e5cc to your computer and use it in GitHub Desktop.
Save mezhaka/6198245f36762e5d332a746e8454e5cc to your computer and use it in GitHub Desktop.
Printing profiling stats example
#!/usr/bin/env python3.4
import cProfile
import pstats
def first():
pass
def second():
for _ in range(100000):
first()
def doit():
for _ in range(100000):
first()
for _ in range(2):
second()
def profile():
profile = cProfile.Profile()
profile.enable()
doit()
profile.disable()
return pstats.Stats(profile)
if __name__ == '__main__':
s = profile()
# print everything
s.sort_stats('cumulative').print_stats()
# print what second called through doit
s.print_callees('doit').print_callees('second')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment