cProfile saving data to file causes jumbles of characters

可紊 提交于 2019-11-29 22:08:52

You should use the pstats module to parse this file and extract information in user-friendly format from it. For example:

import pstats
p = pstats.Stats('thing.txt')
p.sort_stats('cumulative').print_stats(10)

It's all in the documentation, of course. Go over the "instant user's manual" in there, it explains everything.

to dump the stats driectly:

echo 'stats' | python3 -m pstats path/to/cprofile_output_file

pstats also has a shell

$ python3 -m pstats path/to/cprofile_output_file

within we can issue stats or sort commands like so:

$ python3 -m pstats path/to/cprofile_output_file
Welcome to the profile statistics browser.
prof.txt% sort cumtime
prof.txt% reverse
prof.txt% stats

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.000    0.000 63:1(<module>)
        1    0.000    0.000    0.000    0.000 {built-in method builtins.exec}
        1    0.000    0.000    0.000    0.000 {built-in method builtins.print}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

prof.txt% ?

Documented commands (type help <topic>):
========================================
EOF  add  callees  callers  help  quit  read  reverse  sort  stats  strip

a micro-feature I enjoyed here is I get to reverse the sort order natively <3

echo -e 'sort cumtime\nreverse\nstats' | python3 -m pstats path/to/cprofile_output_file
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!