当我使用 pstats
显示分析数据时,第一列是每个函数的调用次数。
但是,当我对数据进行排序时,我可以在 calls
、ncalls
和 pcalls
键之间进行选择。文档说 calls
和 ncalls
是调用计数,而 pcalls
是原始调用计数。按 calls
或 ncalls
排序是一样的吗? 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
按calls
或ncalls
排序是一样的。
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/