r/cpp • • 4h ago

How much variance in benchmarks are normal?

I am currently making some changes to an existing sorting function and I have to benchmark the results in order to merge them. The problem is that, running the same benchmark with the same inputs, with the same number of iterations, yields up to 15% variance on median time on different seperate runs. I am using Google's Benchmark framework, pin the task, disable CPU scaling, disable ASLR, and the variance is still like this. Is this normal for a micro-benchmark? If it is, then it is near impossible to pin-point improvements unless they are huge.

2 Upvotes

8 comments sorted by

2

u/aruisdante 4h ago

It really depends on what you're benchmarking, and how sensitive it is to whatever else is running on your system. Without more information on exactly what you're doing, it's hard to say if this is normal or abnormal behavior.

1

u/brogolem35 4h ago

It is sorting function. The benchmarking code sorts 3 types of arrays (sorted, reverse sorted, seeded random). The benchmarking process takes about an hour per run and I don't touch the PC for that time and no background process runs in the said time frame either. I am trying to think of potential footguns here but having a hard time with it.

•

u/hobel_ 3h ago

One hour? IO involved? May be monitor the CPU frequency if it really is constant or if there is a thermal issue.

•

u/I__Know__Stuff 2h ago

no background process runs in the time frame

Take a look at task manager on Windows or ps axl on Linux to see the hundreds of background processes running.

•

u/kniy 3h ago

Unfortunately benchmarking comes with tons of footguns.

Something I hit recently: Linux transparent huge pages. A large allocation may get 2MB pages, or tons of 4KB pages, depending on how fragmented physical memory is at the time the allocation was made. This can have a major performance impact on programs with frequent TLB misses.

•

u/blipman17 2h ago

Highly depends.

My latest benchmark was being highly influenced by alignment of the generated assembly, so not even data alignment.
In the end I kinda didn't have a way to controll this which would translate nicely to my application where I wanted this algorithm in.

•

u/Slsyyy 3h ago

How long is the single benchmark loop? For an extremely short loop the noise may be a dominant factor

The behavior of memory allocation may be also a reason, if you sort algorithm allocates the memory

•

u/DummyDDD 56m ago

Variations of 15 % sounds like a lot for sorting benchmark where each run takes minutes. I have a couple of questions.

  1. How many runs per type/implementation?
  2. Do you get similar variance in the mean results?
  3. Are you sorting arrays of values, or are you sorting something stored indirectly?
  4. How much memory is your benchmark using and what hardware are you running on?
  5. Is your sorting function parallel?
  6. Are you running on you own computer or some kind of hosted or virtual environment?
  7. How have you disabled CPU scaling?
  8. Could your CPU be overheating or throttling for some other reason?
  9. How long does each run take?
  10. Do you perform all of the runs in one process instantiation, or do you run your executable multiple times / how many runs per process instantiation?
  11. Could your computer be running some kind of crow job, auto upgrade, or just check for updates in the background?
  12. Does your computer have an active network connection during the benchmark?
  13. Could your computer be running heavy background tasks, like browsers, ides or ai agents?
  14. Are you running the benchmark repeatedly on the same memory, or are you allocating new memory per run?

As a general rule of thumb, you should not apply statistics to less than 10 observation per implementation/input.

I have personally had good experience benchmarking with 50 process instantiations per implementation/input type, with each process taking a few seconds (repeating the benchmark to be approximately 5 seconds per instantiation in the baseline implementation) and generating the input with fast random number generators (although, as long as you stay away from rand, most random number generators are plenty fast). I have tried having issues with overheating, which gave weird variations, which I would "solve" by running a warmup before the first instantiation, running a sleep between each instantiation, and moving my computer to the windowsill (it was a laptop). I would also disable all cron jobs, disable network connectivity and run the benchmarks without a window manager. I would also stay physically away from the computer running the benchmark, as you can harm a benchmark just by typing in a text editor.

You can usually reduce the impact of the variation by making more observations. Getting variations of 15 % sounds suspicionsly high, but the high variation could be caused by something related to my previous questions.

If you just need to get a feel for whether something helps, then you don't need 50 process instantiations/observations something like 5 should be fine, but when you want your final number, saying that "X is Y% faster or slower than Z" you should have at least 20 observations per type/input (and I would suggest 50, if you have time for it).