cProfile 将数据保存到文件会导致字符混乱

发布于 2024-12-18 05:34:35 字数 563 浏览 2 评论 0原文

我在名为 bot4CA.py 的模块上使用 cProfile,因此在控制台中输入:

python -m cProfile -o thing.txt bot4CA.py

模块运行并退出后,它会创建一个名为 thing.txt 的文件,当我打开它时,其中有一些信息,其余的是混乱的字符而不是我想要的整齐组织的数据文件。有谁知道如何使用 cProfile 并最终得到组织整齐的数据表,就像在命令行上正常使用它一样,除了在文件中? 下面是 .txt 文件中的一些数据的示例:

{(   s)   build\bdist.win32\egg\colorama\winterm.pyi'   t      reset_all(   i   i   gpàÂs% ?geOÙHÌœE?{(   s-   build\bdist.win32\egg\colorama\ansitowin32.pyi¥

我真正想要的是调用 cProfile.run() 时会发生什么,这会导致打印出一个组织整齐的表格,显示所有函数的执行时间(除了打印之外),保存在一个文件中,因为该程序相当大并且运行很多功能。

I am using cProfile on a module named bot4CA.py so in the console I type:

python -m cProfile -o thing.txt bot4CA.py

After the module runs and exits, it creates a file named thing.txt and when I open it, there is some information there, and the rest is a jumble of characters instead of a neatly organized file of data which is what I want. Does any one know how to use cProfile and end up with neatly organized table of data like when using it normally on command line, except in a file?
Here's an example of some of the data in the .txt file:

{(   s)   build\bdist.win32\egg\colorama\winterm.pyi'   t      reset_all(   i   i   gpàÂs% ?geOÙHÌœE?{(   s-   build\bdist.win32\egg\colorama\ansitowin32.pyi¥

What I really want is what happens when you invoke cProfile.run() which results in a neatly organized table printed showing the execution times of all the functions except instead of printed, saved in a file as this program is fairly large and runs a lot of functions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

沐歌 2024-12-25 05:34:35

您应该使用 pstats 模块来解析此文件并从中提取用户友好格式的信息。例如:

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

当然,这些都在文档中。查看那里的“即时用户手册”,它解释了一切。

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.

吻安 2024-12-25 05:34:35

直接转储统计信息:

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

pstats 中还有一个 shell

$ python3 -m pstats path/to/cprofile_output_file

,我们可以发出 statssort 命令,如下所示:

$ 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

我在这里喜欢的一个微观功能是我可以反转排序原生顺序<3

echo -e 'sort cumtime\nreverse\nstats' | python3 -m pstats path/to/cprofile_output_file

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
束缚m 2024-12-25 05:34:35

其他答案更强大、更灵活,但如果您只想快速输出,请使用 > 而不是 -o。这将以纯文本格式保存报告。

python -m cProfile myscript.py > cprofile.txt
python -m cProfile bot4CA.py > thing.txt

The other answers are more powerful and flexible, but if you just want to get a quick output, use > instead of -o. That will save the report in plain text format.

python -m cProfile myscript.py > cprofile.txt
python -m cProfile bot4CA.py > thing.txt
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文