26.4. The Python Profilers

26.4.1. Introduction to the profilers

A profiler is a program that describes the run time performance of a program, providing a variety of statistics. This documentation describes the profiler functionality provided in the modules cProfile, profile and pstats. This profiler provides deterministic profiling of Python programs. It also provides a series of report generation tools to allow users to rapidly examine the results of a profile operation.

The Python standard library provides three different profilers:

  1. cProfile is recommended for most users; it’s a C extension with reasonable overhead that makes it suitable for profiling long-running programs. Based on lsprof, contributed by Brett Rosen and Ted Czotter.

    New in version 2.5:

    New in version 2.5.

  • profile, a pure Python module whose interface is imitated by cProfile. Adds significant overhead to profiled programs. If you’re trying to extend the profiler in some way, the task might be easier with this module.

    Changed in version 2.4:

    Changed in version 2.4: Now also reports the time spent in calls to built-in functions and methods.

  • hotshot was an experimental C module that focused on minimizing the overhead of profiling, at the expense of longer data post-processing times. It is no longer maintained and may be dropped in a future version of Python.

    Changed in version 2.5:

    Changed in version 2.5: The results should be more meaningful than in the past: the timing core contained a critical bug.