Facundo Casco
Facundo Casco

Reputation: 10585

Python profiler not giving enough information

I am trying to find out why some function is taking a long time to complete.
I am using the profiler like this:

ipdb> import profile
ipdb> profile.runctx('report.generateOutput()', globals(), locals())
         1 function calls in 40.783 CPU seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        0    0.000             0.000          profile:0(profiler)
        1   40.783   40.783   40.783   40.783 profile:0(report.generateOutput())

As you can see, that's not really of much use.
What I need is some detailed information about where all the time is being spent, what am I missing here?

Upvotes: 2

Views: 1326

Answers (2)

Mike Dunlavey
Mike Dunlavey

Reputation: 40649

You say two different things:

  • "What I need is some detailed information about where all the time is being spent"

  • "I am trying to find out why some function is taking a long time to complete"

You see, these are not the same thing. I think it is better to ask why than where, because where is actually very fuzzy.

For example, suppose there is a "bottleneck" consisting of a bubble sort of a big array of strings. Where is the time spent? Is it in the inner loop of the bubble sort, or is it in the string compare? Profilers would say the latter, but the reason why it's there, and the actual problem, is higher up the call stack.

Here's an example of how I do it.

Upvotes: 0

Patrick
Patrick

Reputation: 1291

Profile report.generateOutput(), instead of the function call.

To profile an application with a main entry point of foo(), you would add the following to your module:

import cProfile
cProfile.run('foo()')

Maybe the python docs on profiling are helpful.

Upvotes: 2

Related Questions