views:

240

answers:

1

Hi. I'm used to sampling C-based apps, which every few milliseconds sees what function stack is being called at that moment.

This allows me to see where most of the time is spent in an app so I can optimize it.

When using python, however, sample isn't so helpful, since it's sampling the C functions of the python interpreter, not the python code itself.

Is there a useful sampling tool for python?

+2  A: 

Python includes a built-in set of profiling tools. In particular, you can run cProfile on an arbitrary python script from the command-line:

$ python -m cProfile myscript.py

Much more elaborate usage is available by calling the API directly. Note that the cProfile module was added in Python 2.5. In earlier versions, you can use the pure-Python, but slower "profile" module.

Dave Ray