Question

Possible Duplicate:
How can you profile a Python script?

I have using cProfile to find out what method spent me most of the time on my python code, here is an output after sorting for "percall":

I found that the method len() spent me most of the time:

53515/533   330 930 0   940 00{len}

However, I know that len() call would call the python object __len__() method, how do I know what python class/object spent me the most?

I was using python 2.6.5 and run my script using:

python -m cProfile myscript.py

In fact, my code will use python-numpy, scipy and work on doing numerical optimization using Newton's method. I believe the calculation of the gradient of the objective function spent me most of the time and would like to find out the reason why it spent so much.

Was it helpful?

Solution

cProfile gives you for each function two times: the total time spent inside this function including subcalls and the total time spent inside this function excluding subcalls. To detect bottle necks you typically consider the biggest costs according to those two metrics.

In your case I would look for functions calling len a lot and see if I can store the value somewhere to avoid unecessary calls.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top