Sampling-based profiling is a technique for analyzing program performance by periodically capturing execution snapshots rather than tracking every event. Tools such as perf and gprof implement this approach. The profiler interrupts the program at fixed intervals (e.g., every few milliseconds) and records the current instruction pointer, stack trace, or other relevant data. Over time, frequently sampled locations reveal hotspots—functions or code regions where the program spends most of its execution time. In this method, the profiler sets up a timer to trigger interrupts at regular intervals. At each interrupt, it records where the program is executing (e.g., function address, call stack). The collected samples are then aggregated to estimate where time is spent. Because sampling-based profiling collects data intermittently, it imposes lower overhead than instrumentation-based profiling. This makes it well-suited for long-running programs and complex applications. Additionally, it does not require modifying the code to insert instrumentation. However, a key limitation is that rare events may be missed if they do not coincide with sampling points. The accuracy of results also depends on the sampling frequency—too low may overlook important details, while too high increases overhead.










