Skip to content

Instantly share code, notes, and snippets.

@ralfstx
Created October 8, 2020 07:19
Show Gist options
  • Save ralfstx/a173a7e4c37afa105a66f371a09aa83e to your computer and use it in GitHub Desktop.
Save ralfstx/a173a7e4c37afa105a66f371a09aa83e to your computer and use it in GitHub Desktop.
Python cProfile to CSV function
def prof_to_csv(prof: cProfile.Profile):
out_stream = io.StringIO()
pstats.Stats(prof, stream=out_stream).print_stats()
result = out_stream.getvalue()
# chop off header lines
result = 'ncalls' + result.split('ncalls')[-1]
lines = [','.join(line.rstrip().split(None, 5)) for line in result.split('\n')]
return '\n'.join(lines)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment