Python profiling with cProfile

Compared to Perl's Devel::NYTProf,  Python profilers are a bit rough around the edges. The recommended profiler as of 2.6 is part of the standard library and is called cProfile. It works only at the function  (as opposed to statement) level and fancy graphical output is non existent. Here's a brief overview of what it offers.

Profiling output

For each function, cProfile makes the following data available:

Two different datasets are headed percall. The data is printed by default exactly in the order given above; except in columns from left to right. Keep that in mind for deciphering the ouput.

There are two ways of obtaining this display. If you run the cProfile on a script without specifying an output file, this data will just be printed to the screen. If you specify an output file, you need to use the pstats module to load the data (the actual output file is in some binary format). pstats also offers some methods to sort and filter the data. So if your program is not very small, you are better off using pstats.

and now you can display the information in a manageble way by calling the appropriate methods on the stats object.