Two ways to find out where time goes
Every profiler is built on one of two mechanisms, and they are not variations of the same idea.
Instrumentation records every event. The profiler inserts hooks at function entry and exit, so it knows exactly how many times each function was called and how long each call took. The data is exact and complete.
Sampling interrupts the program at intervals, typically tens to hundreds of times a second, and records the current stack. It does not know how many times anything was called. It knows what the program was doing at a set of moments, and infers proportions statistically.
| Instrumentation | Sampling | |
|---|---|---|
| Data | Exact counts and durations | Statistical proportions |
| Overhead | High, proportional to call count | Low and roughly constant |
| Distortion | Severe on small hot functions | Minimal |
| Production safe | Usually not | Usually yes |
| Best for | Call counts, algorithmic questions | Finding where time actually goes |
Key idea: sampling is the default for production performance work, and the reason is not accuracy but honesty. Its overhead is small enough that the program it measures still behaves like the program you deployed, which is the property that matters most.

