【问题标题】:Sort cProfile output by percall when profiling a Python script分析 Python 脚本时按 percall 对 cProfile 输出进行排序
【发布时间】:2012-05-06 19:16:21
【问题描述】:

我正在使用python -m cProfile -s calls myscript.py

python -m cProfile -s percall myscript.py 不起作用。

Python 文档说“在 Stats 文档中查找有效的排序值。”:http://docs.python.org/library/profile.html#module-cProfile,我找不到。

【问题讨论】:

标签: python profiling cprofile


【解决方案1】:

-s 只使用the keys found under sort_stats.

calls (call count)
cumulative (cumulative time)
cumtime (cumulative time)
file (file name)
filename (file name)
module (file name)
ncalls (call count)
pcalls (primitive call count)
line (line number)
name (function name)
nfl (name/file/line)
stdname (standard name)
time (internal time)
tottime (internal time)

这是一个例子

python -m cProfile -s tottime myscript.py

【讨论】:

  • 只是为了清除它,对于像我这样的菜鸟,你不要使用引号 :) 因此,按方法中花费的时间对方法进行排序的示例是“python -m cProfile -s tottime myscript .py"
  • 使用引号没有任何问题(至少在 bash 中),是吗?引号被 bash 命令预处理吃掉了..
  • 就 windows 而言,不能使用引号。还要注意 - 按时间排序,tottime 是一回事。
  • 我希望能够按提供的任何列对 cprofile 输出进行排序。据我所知,目前还不能按percall列排序。
  • percall 列是什么?您可以对任何列进行排序。 docs.python.org/3/library/profile.html#pstats.Stats.sort_stats 作为最后的手段,您可以使用列的索引For backward-compatibility reasons, the numeric arguments -1, 0, 1, and 2 are permitted.
猜你喜欢
  • 2021-11-01
  • 1970-01-01
  • 2021-12-12
  • 1970-01-01
  • 1970-01-01
  • 2017-06-13
  • 2023-04-06
  • 2014-02-23
  • 1970-01-01
相关资源
最近更新 更多