Search code examples
pythonprofilingperformance-testing

How python's profiler cProfile works? Does it instrument or samples the code?


Also, what are the differences between cProfile and profile?


Solution

  • cProfile is deterministic profiler : trace functions are executed at various points of interest (e.g. function call, function return, exceptions) and precise timings of these events are recorded. You can find more information here.

    From the documentation : The Python standard library provides two different profilers:

    • 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.
    • 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.