diff --git a/examples/google_benchmark_cmake/fixture_bench.hpp b/examples/google_benchmark_cmake/fixture_bench.hpp index ff2d4e3..2feca08 100644 --- a/examples/google_benchmark_cmake/fixture_bench.hpp +++ b/examples/google_benchmark_cmake/fixture_bench.hpp @@ -16,10 +16,12 @@ class MyFixture : public benchmark::Fixture { }; BENCHMARK_F(MyFixture, FooTest)(benchmark::State &st) { for (auto _ : st) { + benchmark::ClobberMemory(); } } BENCHMARK_DEFINE_F(MyFixture, BarTest)(benchmark::State &st) { for (auto _ : st) { + benchmark::ClobberMemory(); } } BENCHMARK_REGISTER_F(MyFixture, BarTest); @@ -31,11 +33,13 @@ template class MyTemplatedFixture : public benchmark::Fixture {}; BENCHMARK_TEMPLATE_F(MyTemplatedFixture, IntTest, int)(benchmark::State &st) { for (auto _ : st) { + benchmark::ClobberMemory(); } } BENCHMARK_TEMPLATE_DEFINE_F(MyTemplatedFixture, DoubleTest, double)(benchmark::State &st) { for (auto _ : st) { + benchmark::ClobberMemory(); } } BENCHMARK_REGISTER_F(MyTemplatedFixture, DoubleTest); @@ -47,6 +51,7 @@ template class MyTemplate1 : public benchmark::Fixture {}; BENCHMARK_TEMPLATE1_DEFINE_F(MyTemplate1, TestA, int)(benchmark::State &st) { for (auto _ : st) { + benchmark::ClobberMemory(); } } BENCHMARK_REGISTER_F(MyTemplate1, TestA); @@ -59,6 +64,7 @@ class MyTemplate2 : public benchmark::Fixture {}; BENCHMARK_TEMPLATE2_DEFINE_F(MyTemplate2, TestB, int, double)(benchmark::State &st) { for (auto _ : st) { + benchmark::ClobberMemory(); } } BENCHMARK_REGISTER_F(MyTemplate2, TestB); diff --git a/examples/google_benchmark_cmake/main.cpp b/examples/google_benchmark_cmake/main.cpp index 035ae45..045bc65 100644 --- a/examples/google_benchmark_cmake/main.cpp +++ b/examples/google_benchmark_cmake/main.cpp @@ -8,8 +8,9 @@ template void BM_Capture(benchmark::State &state, Args &&...args) { auto args_tuple = std::make_tuple(std::move(args)...); - (void)args_tuple; for (auto _ : state) { + benchmark::DoNotOptimize(args_tuple); + benchmark::ClobberMemory(); } } BENCHMARK_CAPTURE(BM_Capture, int_string_test, 42, std::string("abc")); @@ -20,6 +21,8 @@ static void BM_rand_vector(benchmark::State &state) { std::vector v; for (auto _ : state) { std::string empty_string; + benchmark::DoNotOptimize(empty_string); + benchmark::ClobberMemory(); } } // Register the function as a benchmark @@ -30,6 +33,8 @@ static void BM_StringCopy(benchmark::State &state) { std::string x = "hello"; for (auto _ : state) { std::string copy(x); + benchmark::DoNotOptimize(copy); + benchmark::ClobberMemory(); } } // Register the function as a benchmark @@ -39,7 +44,11 @@ static void BM_memcpy(benchmark::State &state) { char *src = new char[state.range(0)]; char *dst = new char[state.range(0)]; memset(src, 'x', state.range(0)); - for (auto _ : state) memcpy(dst, src, state.range(0)); + for (auto _ : state) { + memcpy(dst, src, state.range(0)); + benchmark::DoNotOptimize(dst); + benchmark::ClobberMemory(); + } delete[] src; delete[] dst; } diff --git a/examples/google_benchmark_cmake/template_bench.hpp b/examples/google_benchmark_cmake/template_bench.hpp index 0625222..b25b8b4 100644 --- a/examples/google_benchmark_cmake/template_bench.hpp +++ b/examples/google_benchmark_cmake/template_bench.hpp @@ -10,6 +10,8 @@ template void BM_Template(benchmark::State &state) { std::vector v; for (auto _ : state) { v.push_back(T()); + benchmark::DoNotOptimize(v); + benchmark::ClobberMemory(); } } BENCHMARK_TEMPLATE(BM_Template, int); @@ -46,6 +48,8 @@ template void BM_Template1_Capture(benchmark::State &state, ExtraArgs &&...extra_args) { auto args_tuple = std::make_tuple(std::move(extra_args)...); for (auto _ : state) { + benchmark::DoNotOptimize(args_tuple); + benchmark::ClobberMemory(); } } BENCHMARK_TEMPLATE1_CAPTURE(BM_Template1_Capture, void, int_string_test, 42,