【问题标题】:cProfile saving data to file causes jumbles of characterscProfile 将数据保存到文件会导致字符混乱
【发布时间】:2012-01-07 03:51:19
【问题描述】:

我在名为 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() 时会发生什么,它会打印出一个组织整齐的表格,显示所有函数的执行时间,除了打印之外,保存在一个文件中,因为这个程序相当大并且运行很多功能。

【问题讨论】:

    标签: python file command-line profiling cprofile


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      直接转储统计数据:

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

      pstats 也有一个外壳

      $ 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
      

      我在这里喜欢的一个微功能是我可以在本地反转排序顺序

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

      【讨论】:

      • 这是一个非常古老的问题的新答案,但在我看来,它比接受的答案更有帮助,因为它与 OP 的命令行 cProfile 用法相匹配。令人讨厌的是,官方文档并没有在演示如何生成它的任何地方演示解析输出文件。 +1
      • 还有:echo -e 'sort tottime\nreverse\nstats' | python3 -m pstats &lt;(python3 -m cProfile -o /dev/stdout ./myscript.py myargs)
      • 这很有帮助。在我的 Windows 机器上,第一部分 echo 语句没有引号。使用(例如)echo stats | python -m pstats path\to\profile_output
      【解决方案3】:

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

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

      当然都是in the documentation。翻阅那里的“即时用户手册”,它解释了一切。

      【讨论】:

      • 如何使用命令行来做到这一点:python -m cProfile -o thing.txt bot4CA.py。原因是 cProfile 似乎不想运行整个脚本。
      • 好吧,现在我有第二个问题,这个程序使用了twisted,并且数据只和twisted的主循环一样深,而不是脚本中twisted调用的方法
      • @joseph:链接的文档解释了命令行的使用,尽管它提供的报告选项相对有限
      • 对于扭曲的问题,它只给出扭曲的主循环或该级别的东西的时间,而不是扭曲的框架调用的方法,如 dataRecieved?
      • @joseph:我不知道 - 从未尝试过分析 Twisted。您可以打开一个单独的问题或在一些 Twisted 论坛/列表中提问
      猜你喜欢
      • 2015-06-15
      • 1970-01-01
      • 2012-02-03
      • 2019-01-07
      • 1970-01-01
      • 2018-03-01
      • 1970-01-01
      • 2015-01-28
      相关资源
      最近更新 更多