
Why Naive Timing Loops Lie: Common Benchmarking Mistakes
A timing loop around a function can report zero, the wrong function or noise. Learn seven mistakes that make native benchmarks lie, with fixes.
Allocators, cache-friendly data layout, profiling with perf, VTune and Instruments, benchmarking that means something, and memory safety without losing speed.

A timing loop around a function can report zero, the wrong function or noise. Learn seven mistakes that make native benchmarks lie, with fixes.

Set kernel.perf_event_paranoid, kernel.kptr_restrict and CAP_PERFMON so perf can sample user and kernel code without running everything as root.

Use perf annotate to see which assembly instructions and source lines inside a hot function receive the most samples, and how to read skid correctly.

Build the perf tool from the Linux source tree with make -C tools/perf, check the feature report, and install it so its version matches the kernel you run.

Sample a program with perf record, rank functions with perf report, and read the Overhead, Command and Symbol columns to find the code that burns your CPU time.

Count cycles, instructions and cache misses with perf stat, then read instructions per cycle and miss ratios to tell compute from memory stalls.

Install perf from your distribution, check it matches the kernel, then run perf stat and perf record on a small C program to see where time goes.

Rewrite float reductions, indirect gathers and early-exit loops in C so GCC and Clang vectorize them, using partial sums and blocked scans.

Turn on auto-vectorization in GCC and Clang, read the optimization reports that say why a loop was skipped, and set the flags that matter, including -march.

Read each column of /proc/interrupts, find which CPU serves a device, and pin an IRQ to chosen CPUs with smp_affinity_list on Linux 6.12.

Use arm_neon.h to write NEON vector loops in C for AArch64: float32x4_t, vld1q_f32, vaddq_f32, vfmaq_f32 and vaddvq_f32, with a full dot product and its output.

Convert a 128-bit SSE integer loop to 256-bit AVX2: new types, a step of eight, the -mavx2 flag, runtime dispatch and the lane-crossing trap.
We send one email when a new post is published. No spam.