Harrison Morgan
Harrison Morgan

Reputation: 13

What does this Python cProfile output mean?

ncalls  tottime  percall  cumtime  percall filename:lineno(function)
    1    0.000    0.000   65.417   65.417 <string>:1(<module>)
    1   43.675   43.675   65.417   65.417 primenumber_o.py:3(main)
    1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
99999   21.742    0.000   21.742    0.000 {range}
    2    0.000    0.000    0.000    0.000 {time.time}

Specifically, the third line. I read up on cProfile, but nothing explained what that line means. It also doesn't provide any keywords I could search for on Google, so I'm stumped.

The Python script I'm profiling finds prime numbers.

I see that 21 seconds is spent in the loop, on line 5. What I don't understand is what the other 43 seconds is doing.

Upvotes: 0

Views: 1287

Answers (2)

Amber
Amber

Reputation: 526623

The third line is all of the code in the file that isn't range().

Upvotes: 2

Lee D
Lee D

Reputation: 116

I haven't used cProfile, but it looks to me that it assigns runtime into buckets based on function names. 21 seconds is being used by the built-in function range() in the for statement. The other 43 seconds isn't in a separate, named function, so the name it is considered to fall within is main, as in:

if __name__ == '__main__':
    ...

If you put the body of the loop into a function, the 43 seconds (or most of it) will show up there. If you split up the body into multiple functions, you'll get more fine-grained profiling.

Upvotes: 0

Related Questions