5.1. Profiling About

5.1.1. Built-in Profilers

  • profiling.sampling: statistical sampling. Best for production profiling, CPU analysis, I/O bottlenecks, multithreaded applications and low-overhead performance investigation.

  • profiling.tracing: deterministic tracing. Best for exact function-call counts, call graphs and detailed function-level execution analysis.

  • Since Python 3.15 python -m profile myfile.py is deprecated

  • Since Python 3.15 python -m cProfile myfile.py was renamed to python -m profiling.tracing myfile.py

Python 3.15 introduces a new built-in profiling package, profiling, which consolidates deterministic profiling and adds a new high-frequency statistical sampling profiler called Tachyon.

The two profilers serve different purposes:

  • profiling.sampling: statistical sampling. Best for production profiling, CPU analysis, I/O bottlenecks, multithreaded applications and low-overhead performance investigation.

  • profiling.tracing: deterministic tracing. Best for exact function-call counts, call graphs and detailed function-level execution analysis.

Both are included in Python 3.15. You do not need to install third-party packages.

5.1.2. Sampling vs Tracing

  • Sampling results are statistical estimates. A short-running function might not appear in the profile.

  • Tracing introduces overhead. Avoid interpreting traced execution times as representative production timings.

  • Wall-clock and CPU-time profiles answer different questions. A function waiting for network I/O can dominate wall-clock time while consuming almost no CPU.

  • Native extensions require particular attention. Python-level tracing does not provide the same visibility into native execution as sampling with --native.

  • Profilers are not benchmarking tools. Use repeated, controlled benchmarks to measure actual performance improvements.

Basic Usage:

$ python -m profiling.sampling myfile.py
$ python -m profiling.tracing myfile.py
| Property              | Sampling           | Tracing                         |
|-----------------------|--------------------|---------------------------------|
| Module                | profiling.sampling | profiling.tracing               |
| Method                | Statistical        | Deterministic                   |
| Function call counts  | Estimated          | Exact                           |
| Overhead              | Very low           | Moderate                        |
| Production profiling  | Excellent          | Less suitable                   |
| Short-lived functions | Can miss           | Captures                        |
| Call hierarchy        | Yes                | Yes                             |
| Flame graphs          | Yes                | No built-in support             |
| Native C execution    | Yes, with --native | Limited                         |
| Attach to PID         | Yes                | No                              |
| GIL analysis          | Yes                | No                              |
| Thread analysis       | Yes                | Yes, subject to instrumentation |

5.1.3. Further Reading