Skip to content

Instantly share code, notes, and snippets.

@sean-adler
Created April 3, 2018 19:29
Show Gist options
  • Save sean-adler/6f493b6741cc792c1cbe24c896c54344 to your computer and use it in GitHub Desktop.
Save sean-adler/6f493b6741cc792c1cbe24c896c54344 to your computer and use it in GitHub Desktop.
Write cProfile stats to human-readable text file
import cProfile
import pstats
prof = cProfile.Profile()
prof.run('<YOUR CODE>')
prof.sort_stats('cumtime')
prof.dump_stats('output.prof')
stream = open('output.txt', 'w')
stats = pstats.Stats('output.prof', stream=stream)
stats.sort_stats('cumtime')
stats.print_stats()
@jeffxhansen
Copy link

I had to leave out line 6 for my code to work.

@ppsic
Copy link

ppsic commented May 24, 2024

@jeffxhansen it worked for me - the line for input stream seems to be missing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment