Perf Counters
ELI5 - The Vibe Check
Perf counters are the CPU's own built-in scorekeepers, tiny hardware registers that silently count things like cache misses, branch mispredictions, and instructions executed, without slowing your program down at all. Tools like Linux's perf just read these numbers straight off the chip, so you get an answer to is this slow because of cache misses or because of the algorithm without any guessing.
Real Talk
Performance counters (hardware performance counters) are special-purpose registers built into the CPU that count low-level microarchitectural events like cache misses, branch mispredictions, instructions retired, and cycles stalled. Profiling tools like Linux perf, VTune, or perf_event_open read these counters directly from hardware, giving near-zero-overhead insight into exactly why code is slow at the CPU level.
Show Me The Code
perf stat -e cache-misses,branch-misses,instructions ./my_program
When You'll Hear This
"perf stat showed a huge cache-miss rate, that's the false sharing you suspected." / "Hardware counters told us it was branch mispredictions, not the algorithm."
Related Terms
Branch Prediction
Modern CPUs don't wait around to find out if an if statement is true, they guess the answer and start executing down that path before they even know for su...
Cache Line
Your CPU never fetches just the one byte you asked for, it grabs a whole chunk around it, usually 64 bytes, called a cache line, on the theory that you'll...
Flame Graph
A flame graph is a picture of where your program actually spent its time, and it looks like a skyline made of stacked flames.
Profiling
Profiling is watching your program run and keeping score of where the time and memory actually go, instead of guessing.