When compiling the benchmark code below with -O3
I was impressed by the difference it made in latency so i began to wonder whether the compiler is not \"cheatin
You should always benchmark with optimizations turned on. However it is important to make sure the things you want to time do not get optimized away by the compiler.
One way to do this by printing out calculation results after the timer has stopped:
long long x = 0;
for(int i = 0; i < iterations; i++) {
long start = get_nano_ts(); // START clock
for(int j = 0; j < load; j++) {
if (i % 4 == 0) {
x += (i % 4) * (i % 8);
} else {
x -= (i % 16) * (i % 32);
}
}
long end = get_nano_ts(); // STOP clock
// now print out x so the compiler doesn't just ignore it:
std::cout << "check: " << x << '\n',
// (omitted for clarity)
}
When comparing benchmarks for several different algorithms that can also serve as a check that each algorithm is producing the same results.