Jest jakaś większa różnica pomiędzy użyciem
for (auto _ : state)
a
while (state.KeepRunning())
?
generalnie for (auto _ : state)
zawsze jest szybszy
i czasami wynik wychodzi mi "0ns" moze optymalizator , tak zoptymalizował ze nic nie zostało
Albo test durny, to i wyniki też durne ?
https://godbolt.org/z/6rd3cYe3e
#include <benchmark/benchmark.h>
#include <spdlog/spdlog.h>
static void i32_addition(benchmark::State &state) {
int32_t a, b, c;
a=1;
b=2;
c=3;
for (auto _ : state)
{
c = c+ a + b;
}
(void)c;//spdlog::info("{}",c);
}
BENCHMARK(i32_addition)->Repetitions(3)->Iterations(100000);
static void i32_addition2(benchmark::State &state) {
int32_t a, b, c;
a=1;
b=2;
c=3;
while (state.KeepRunning())
{
c = c+ a + b;
}
(void)c;// spdlog::info("{}",c);
}
BENCHMARK(i32_addition2)->Repetitions(3)->Iterations(100000);
BENCHMARK_MAIN();
-------------------------------------------------------------------------------------------
Benchmark Time CPU Iterations
-------------------------------------------------------------------------------------------
i32_addition/iterations:100000/repeats:3 2.20 ns 2.18 ns 100000
i32_addition/iterations:100000/repeats:3 2.74 ns 2.74 ns 100000
i32_addition/iterations:100000/repeats:3 1.95 ns 1.95 ns 100000
i32_addition/iterations:100000/repeats:3_mean 2.30 ns 2.29 ns 3
i32_addition/iterations:100000/repeats:3_median 2.20 ns 2.18 ns 3
i32_addition/iterations:100000/repeats:3_stddev 0.404 ns 0.408 ns 3
i32_addition/iterations:100000/repeats:3_cv 17.59 % 17.81 % 3
i32_addition2/iterations:100000/repeats:3 5.38 ns 5.38 ns 100000
i32_addition2/iterations:100000/repeats:3 3.13 ns 3.13 ns 100000
i32_addition2/iterations:100000/repeats:3 3.58 ns 3.59 ns 100000
i32_addition2/iterations:100000/repeats:3_mean 4.03 ns 4.03 ns 3
i32_addition2/iterations:100000/repeats:3_median 3.58 ns 3.59 ns 3
i32_addition2/iterations:100000/repeats:3_stddev 1.19 ns 1.19 ns 3
i32_addition2/iterations:100000/repeats:3_cv 29.49 % 29.49 % 3