Skip to content
systemcoding.dev
Performance

Repeat a measurement and get variance with perf stat -r 5

Run a command several times and let perf stat print each counter as a mean with its standard deviation, using -r 5 on perf from Linux 6.12.

1 min read
Summarize in AI

A single perf stat run is one sample, and one sample tells you nothing about noise. Add -r 5 and perf runs the command five times, then reports each counter as a mean followed by the relative standard deviation in parentheses. That figure is the quick test of whether a before-and-after difference is real.

Shell
perf stat -r 5 -e task-clock,cycles,instructions ./sum 100000000
Text
 Performance counter stats for './sum 100000000' (5 runs):

            412.80 msec task-clock                       #    0.998 CPUs utilized               ( +-  0.31% )
     1,502,334,118      cycles                           #    3.639 GHz                         ( +-  0.28% )
     3,610,221,904      instructions                     #    2.40  insn per cycle              ( +-  0.02% )

          0.413601 +- 0.000912 seconds time elapsed  ( +-  0.22% )

The counts above are an example from the code's shape and will differ on your machine. What matters is the +- column: if a change moves the mean by less than the spread, you have measured noise. The program's own stdout is printed on every run, so redirect it with > /dev/null inside a sh -c wrapper when it is chatty. -r is documented in the perf-stat(1) man page that ships with Linux 6.12. Five runs is a floor, not a target; slow commands justify fewer and noisy ones justify many more.

Taggedlinuxperf
Share

More snippets

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.