You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
1. :mod:`cProfile` is recommended for most users; it's a C extension with
46
+
.. note::
47
+
48
+
The statistical profiler (:mod:`profile.sample`) is recommended for most production
49
+
use cases due to its extremely low overhead and ability to profile running processes
50
+
without modification. It can attach to any Python process and collect performance
51
+
data with minimal impact on execution speed, making it ideal for debugging
52
+
performance issues in live applications.
53
+
54
+
The Python standard library provides three different profiling implementations:
55
+
56
+
**Statistical Profiler:**
57
+
58
+
1. :mod:`profile.sample` provides statistical profiling of running Python processes
59
+
using periodic stack sampling. It can attach to any running Python process without
60
+
requiring code modification or restart, making it ideal for production debugging.
61
+
62
+
**Deterministic Profilers:**
63
+
64
+
2. :mod:`cProfile` is recommended for development and testing; it's a C extension with
29
65
reasonable overhead that makes it suitable for profiling long-running
30
66
programs. Based on :mod:`lsprof`, contributed by Brett Rosen and Ted
31
67
Czotter.
32
68
33
-
2. :mod:`profile`, a pure Python module whose interface is imitated by
69
+
3. :mod:`profile`, a pure Python module whose interface is imitated by
34
70
:mod:`cProfile`, but which adds significant overhead to profiled programs.
35
71
If you're trying to extend the profiler in some way, the task might be easier
36
72
with this module. Originally designed and written by Jim Roskind.
@@ -44,6 +80,22 @@ profiling interface:
44
80
but not for C-level functions, and so the C code would seem faster than any
45
81
Python one.
46
82
83
+
.. _statistical-profiling:
84
+
85
+
What Is Statistical Profiling?
86
+
==============================
87
+
88
+
:dfn:`Statistical profiling` works by periodically interrupting a running program to capture its current call stack. Rather than monitoring every function entry and exit like deterministic profilers, it takes snapshots at regular intervals to build a statistical picture of where the program spends its time.
89
+
90
+
The sampling profiler uses process memory reading (via system calls like `process_vm_readv` on Linux, `vm_read` on macOS, and `ReadProcessMemory` on Windows) to attach to a running Python process and extract stack trace information without requiring any code modification or restart of the target process. This approach provides several key advantages over traditional profiling methods.
91
+
92
+
The fundamental principle is that if a function appears frequently in the collected stack samples, it is likely consuming significant CPU time. By analyzing thousands of samples, the profiler can accurately estimate the relative time spent in different parts of the program. The statistical nature means that while individual measurements may vary, the aggregate results converge to represent the true performance characteristics of the application.
93
+
94
+
Since statistical profiling operates externally to the target process, it introduces virtually no overhead to the running program. The profiler process runs separately and reads the target process memory without interrupting its execution. This makes it suitable for profiling production systems where performance impact must be minimized.
95
+
96
+
The accuracy of statistical profiling improves with the number of samples collected. Short-lived functions may be missed or underrepresented, while long-running functions will be captured proportionally to their execution time. This characteristic makes statistical profiling particularly effective for identifying the most significant performance bottlenecks rather than providing exhaustive coverage of all function calls.
97
+
98
+
Statistical profiling excels at answering questions like "which functions consume the most CPU time?" and "where should I focus optimization efforts?" rather than "exactly how many times was this function called?" The trade-off between precision and practicality makes it an invaluable tool for performance analysis in real-world applications.
47
99
48
100
.. _profile-instant:
49
101
@@ -54,6 +106,18 @@ This section is provided for users that "don't want to read the manual." It
54
106
provides a very brief overview, and allows a user to rapidly perform profiling
55
107
on an existing application.
56
108
109
+
**Statistical Profiling (Recommended for Production):**
0 commit comments