5.1. Profiling About
https://docs.python.org/3/whatsnew/3.15.html#tachyon-high-frequency-statistical-sampling-profiler
Tracing profilers record every function call and return, which can be very slow and produce large amounts of data.
Sampling profilers take periodic samples of the program's state, which is less intrusive and produces smaller amounts of data.
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.pyis deprecatedSince Python 3.15
python -m cProfile myfile.pywas renamed topython -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
https://creators.spotify.com/pod/profile/corepy/episodes/The-Megahertz-e35ffoi
https://docs.python.org/3/howto/perf_profiling.html#perf-profiling
https://docs.python.org/3/library/sys.monitoring.html#module-sys.monitoring
https://docs.python.org/3/whatsnew/3.14.html#pep-768-safe-external-debugger-interface-for-cpython
https://docs.python.org/3/whatsnew/3.15.html#tachyon-high-frequency-statistical-sampling-profiler