r/cpp • u/brogolem35 • 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
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.
•
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.
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).