Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Python cProfile, what is the difference between calls count and primitive calls count?

When I display profiling data using pstats, the first column is the number of calls for each function.

However, when I sort data, I have choice between calls, ncalls and pcalls keys. Documentation says that calls and ncalls are call count, when pcalls is primitive call count. Is sorting by calls or ncalls is the same? What is different with pcalls?

like image 320
Charles Brunet Avatar asked Apr 04 '13 16:04

Charles Brunet


People also ask

What is a primitive call Python?

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. Sorting by calls or ncalls is the same.

How does Python cProfile work?

cProfile: A deterministic profiler It works by tracing every function call in a program. That's why it's a deterministic profiler: if you run it with the same inputs it'll give the same output. By default cProfile measures wallclock time—how much time elapsed during the function run.


1 Answers

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

Sorting by calls or ncalls is the same.


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)
like image 198
Pavel Anossov Avatar answered Oct 14 '22 19:10

Pavel Anossov