Skip to content

Commit 43ae986

Browse files
committed
fix: prevent optimizing out the benchmarks
1 parent fa3529d commit 43ae986

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

examples/google_benchmark_cmake/fixture_bench.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@ class MyFixture : public benchmark::Fixture {
1616
};
1717
BENCHMARK_F(MyFixture, FooTest)(benchmark::State &st) {
1818
for (auto _ : st) {
19+
benchmark::DoNotOptimize(st);
20+
benchmark::ClobberMemory();
1921
}
2022
}
2123
BENCHMARK_DEFINE_F(MyFixture, BarTest)(benchmark::State &st) {
2224
for (auto _ : st) {
25+
benchmark::DoNotOptimize(st);
26+
benchmark::ClobberMemory();
2327
}
2428
}
2529
BENCHMARK_REGISTER_F(MyFixture, BarTest);
@@ -31,11 +35,15 @@ template <typename T>
3135
class MyTemplatedFixture : public benchmark::Fixture {};
3236
BENCHMARK_TEMPLATE_F(MyTemplatedFixture, IntTest, int)(benchmark::State &st) {
3337
for (auto _ : st) {
38+
benchmark::DoNotOptimize(st);
39+
benchmark::ClobberMemory();
3440
}
3541
}
3642
BENCHMARK_TEMPLATE_DEFINE_F(MyTemplatedFixture, DoubleTest, double)
3743
(benchmark::State &st) {
3844
for (auto _ : st) {
45+
benchmark::DoNotOptimize(st);
46+
benchmark::ClobberMemory();
3947
}
4048
}
4149
BENCHMARK_REGISTER_F(MyTemplatedFixture, DoubleTest);
@@ -47,6 +55,8 @@ template <typename T>
4755
class MyTemplate1 : public benchmark::Fixture {};
4856
BENCHMARK_TEMPLATE1_DEFINE_F(MyTemplate1, TestA, int)(benchmark::State &st) {
4957
for (auto _ : st) {
58+
benchmark::DoNotOptimize(st);
59+
benchmark::ClobberMemory();
5060
}
5161
}
5262
BENCHMARK_REGISTER_F(MyTemplate1, TestA);
@@ -59,6 +69,8 @@ class MyTemplate2 : public benchmark::Fixture {};
5969
BENCHMARK_TEMPLATE2_DEFINE_F(MyTemplate2, TestB, int, double)
6070
(benchmark::State &st) {
6171
for (auto _ : st) {
72+
benchmark::DoNotOptimize(st);
73+
benchmark::ClobberMemory();
6274
}
6375
}
6476
BENCHMARK_REGISTER_F(MyTemplate2, TestB);

examples/google_benchmark_cmake/main.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
template <class... Args>
1010
void BM_Capture(benchmark::State &state, Args &&...args) {
1111
auto args_tuple = std::make_tuple(std::move(args)...);
12-
(void)args_tuple;
1312
for (auto _ : state) {
13+
benchmark::DoNotOptimize(args_tuple);
14+
benchmark::ClobberMemory();
1415
}
1516
}
1617
BENCHMARK_CAPTURE(BM_Capture, int_string_test, 42, std::string("abc"));
@@ -21,6 +22,8 @@ static void BM_rand_vector(benchmark::State &state) {
2122
std::vector<int> v;
2223
for (auto _ : state) {
2324
std::string empty_string;
25+
benchmark::DoNotOptimize(empty_string);
26+
benchmark::ClobberMemory();
2427
}
2528
}
2629
// Register the function as a benchmark
@@ -31,6 +34,8 @@ static void BM_StringCopy(benchmark::State &state) {
3134
std::string x = "hello";
3235
for (auto _ : state) {
3336
std::string copy(x);
37+
benchmark::DoNotOptimize(copy);
38+
benchmark::ClobberMemory();
3439
}
3540
}
3641
// Register the function as a benchmark
@@ -40,7 +45,11 @@ static void BM_memcpy(benchmark::State &state) {
4045
char *src = new char[state.range(0)];
4146
char *dst = new char[state.range(0)];
4247
memset(src, 'x', state.range(0));
43-
for (auto _ : state) memcpy(dst, src, state.range(0));
48+
for (auto _ : state) {
49+
memcpy(dst, src, state.range(0));
50+
benchmark::DoNotOptimize(dst);
51+
benchmark::ClobberMemory();
52+
}
4453
delete[] src;
4554
delete[] dst;
4655
}

examples/google_benchmark_cmake/template_bench.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ void BM_Template(benchmark::State &state) {
1212
std::vector<T> v;
1313
for (auto _ : state) {
1414
v.push_back(T());
15+
benchmark::DoNotOptimize(v);
16+
benchmark::ClobberMemory();
1517
}
1618
}
1719
BENCHMARK_TEMPLATE(BM_Template, int);
@@ -50,6 +52,8 @@ template <typename T, class... ExtraArgs>
5052
void BM_Template1_Capture(benchmark::State &state, ExtraArgs &&...extra_args) {
5153
auto args_tuple = std::make_tuple(std::move(extra_args)...);
5254
for (auto _ : state) {
55+
benchmark::DoNotOptimize(args_tuple);
56+
benchmark::ClobberMemory();
5357
}
5458
}
5559
BENCHMARK_TEMPLATE1_CAPTURE(BM_Template1_Capture, void, int_string_test, 42,

0 commit comments

Comments
 (0)