'cProfile': the lsprof profiler

This svn repository directory contains 'cProfile', a C replacement for the standard 'profile' module. It should work with any not-too-old version of Python. It is now included in the standard library of Python 2.5.

Therefore, the situation of the profilers in Python is as follows:

'profile.Profile' is the ages-old pure Python profiler. At the end of a run, it builds a dict that is inspected by 'pstats.Stats'. It has some recent support for profiling C calls, which however make it crash in some cases [1]. And of course it's slow (makes a run take about 10x longer).

'hotshot', new from 2.2, is quite faster (reportedly, only 30% added overhead). The log file is then loaded and turned into an instance of the same 'pstats.Stats'. This loading takes ages. The reason is that the log file only records events, and loading is done by instantiating a 'profile.Profile' and sending it all the events. In other words, it takes exactly as long as the time it saved in the first place! Moreover, for some reasons, the results given by hotshot seem sometimes quite wrong. (I don't understand why, but I've seen it myself, and it's been reported by various people, e.g. [2].) 'hotshot' doesn't know about C calls, but it can log line events, although this information is lost(!) in the final conversion to a 'pstats.Stats'.

'lsprof' is a third profiler by Brett Rosen and Ted Czotter, posted on SF in June 2005 [2]. Michael Hudson and I did some minor clean-ups and improvements on it, and it seems to be quite useful. It is, for example, the only of the three profilers that managed to give sensible information about the PyPy translation process without crashing, allowing us to accelerate it from over 30 to under 20 minutes. The SF patch contains a more detailed account on the reasons for writing 'lsprof'. The current version [3] supports C calls but not line events. It has its own simple internal interface, which is wrapped in an interface compatible with 'profile' by a module called 'cProfile'. However, unlike the other two profilers, it can record detailed stats about children, which I found quite useful (e.g. how much time is spent in a function when it is called by another specific function).

'lsprof' has been added to the standard library of Python 2.5. Its public interface is in 'cProfile', which is designed to be a drop-in replacement for 'profile'. The 'pstats' module has also been extended in 2.5 to show the extra children statistics recorded by 'cProfile', if present. In the stand-alone version of lsprof/cProfile that you can download from [3], the extended pstats module is called 'pstats2' to avoid collision with the standard 'pstats'. The easiest way to read the documentation of cProfile is by looking at the Python 2.5 documentation [4].

-+- Armin