[Tutor] Understanding cProfile output

Steven D'Aprano steve at pearwood.info
Thu Sep 20 21:56:12 CEST 2012

On 21/09/12 04:58, ranveer raghuwanshi wrote:
> Hi,
> I am trying to understand the output of cProfile when run against my python
> code. The code is:
> What the above code does is it counts the number of prime numbers less than
> 1,00,000.
> Now when I profile this code using *python -m cProfile -s time
> countPrime.py. *The output I get is  http://sprunge.us/SOEj.

The output is 13 lines. Please don't waste our time, and yours, publishing it
on the web. For trivially small amounts of text like that, just paste it into
the body of your email, like this:

          90414 function calls in 16.705 seconds

    Ordered by: internal time

    ncalls  tottime  percall  cumtime  percall filename:lineno(function)
         1   16.374   16.374   16.705   16.705 countPrime.py:1(<module>)
     90407    0.320    0.000    0.320    0.000 {method 'append' of 'list' objects}
         2    0.011    0.005    0.011    0.005 {range}
         1    0.000    0.000    0.000    0.000 {math.sqrt}
         1    0.000    0.000    0.000    0.000 {math.floor}
         1    0.000    0.000    0.000    0.000 {len}
         1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

This ensures that:

* even when the pastebin expires, or the website is shut down, the email
   archives will include the information necessary to understand your

* people who have access to email but not the web (or their web access
   is restricted) can still understand your question, or respond

> Now what I
> don't understand is what it means by *tottime=16.374 for function
> countPrime.py:1(<module>). *I understand fine that it took around *0.320s
> for method append.*
> So, is 16.374 the total time my scripts takes but according to profiler the
> total time is 16.705.

I'm not entirely sure, I'd need to look at the source code of the profiler,
but I expect that the discrepancy is probably due to the difference between
elapsed wall-clock time and processor time. In other words:

* it took 16.705 seconds of actual, physical time (as measured by a clock
   on the wall) to run your script;

* but only 16.374 seconds of that was time spent by the processor actually
   executing your code; the other 0.331 was probably taken up by other
   processes running on your system, or the operating system itself.


More information about the Tutor mailing list