Understand pyinstrument's statistical profiling mechanism
mainPyinstrument is a statistical profiler, not a tracing profiler. Instead of tracking every function call, it interrupts the program at a configured interval (defaulting to 1ms) and records the entire call stack.
Key advantages:
- Low Overhead: Significantly lower overhead compared to tracing profilers like
cProfileorprofile, which can distort results by slowing down code that makes frequent function calls. - Accuracy: Even with fewer samples, accuracy is maintained because long-running function calls are recorded at the end of the call, effectively 'bunching' samples.
- Full-stack Recording: Unlike
cProfilewhich provides a flat list of functions, pyinstrument records the entire stack, making it easier to understand the context of why expensive calls are being made. It also hides library frames by default to focus on your application code.