python - 在 Python cProfile 中,调用计数和原始调用计数有什么区别?

标签 python profiling

当我使用 pstats 显示分析数据时,第一列是每个函数的调用次数。

但是,当我对数据进行排序时,我可以在 callsncallspcalls 键之间进行选择。文档说 callsncalls调用计数,而 pcalls原始调用计数。按 callsncalls 排序是一样的吗? pcalls 有什么不同?

最佳答案

http://docs.python.org/2/library/profile.html#module-cProfile

We define primitive to mean that the call was not induced via recursion.

...when the function does not recurse, these two values are the same

callsncalls 排序是一样的。


When there are two numbers in the first column (for example, 43/3), then the latter is the number of primitive calls, and the former is the actual number of calls. Note that when the function does not recurse, these two values are the same, and only the single figure is printed:

In [43]: def a(i):
   ....:     if i == 0:
   ....:         return
   ....:     a(i-1)
   ....:


In [54]: %prun a(0)
  ncalls  tottime  percall  cumtime  percall filename:lineno(function)
       1    0.000    0.000    0.000    0.000 <ipython-input-43-25b7f3d268b8>:1(a)


In [55]: %prun a(1)
  ncalls  tottime  percall  cumtime  percall filename:lineno(function)
     2/1    0.000    0.000    0.000    0.000 <ipython-input-43-25b7f3d268b8>:1(a)


In [56]: %prun a(3)
  ncalls  tottime  percall  cumtime  percall filename:lineno(function)
     4/1    0.000    0.000    0.000    0.000 <ipython-input-43-25b7f3d268b8>:1(a)

关于python - 在 Python cProfile 中,调用计数和原始调用计数有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15816415/

相关文章:

python - urlparse.urljoin() 不处理无效的父目录

python - Django 无法加载静态文件

python - 为什么 collections.MutableSet 不提供更新方法?

python - 如何从 plac(Python 命令行解析器)显示默认用法/帮助

python - 谁吃了我的 Python 内存?

performance - Spark : Inconsistent performance number in scaling number of cores

python - 如何通过删除连字符 "-"来恢复分割的单词,因为使用 python 的段落中存在连字符

c - 分析使用不同参数调用的同一 C 函数

cuda - 如何观察可执行文件的一部分的 CUDA 事件和指标(例如,仅在内核执行期间)?

profiling - 是否有适用于 Windows 脚本宿主代码的分析器?