Skip to content

Commit

Permalink
Fix examples with deprecated DoNotOptimize API
Browse files Browse the repository at this point in the history
The const-reference API to DoNotOptimize was deprecated with google#1493. Some
examples in the user guide are using exactly that deprecated interface.
This fixes that by passing non-const lvalues instead. Fixes google#1566
  • Loading branch information
mjacobse committed Mar 7, 2023
1 parent f5bc276 commit 0111636
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions docs/user_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,8 @@ the performance of `std::vector` initialization for uniformly increasing sizes.
static void BM_DenseRange(benchmark::State& state) {
for(auto _ : state) {
std::vector<int> v(state.range(0), state.range(0));
benchmark::DoNotOptimize(v.data());
auto data = v.data();
benchmark::DoNotOptimize(data);
benchmark::ClobberMemory();
}
}
Expand Down Expand Up @@ -492,7 +493,8 @@ static void BM_StringCompare(benchmark::State& state) {
std::string s1(state.range(0), '-');
std::string s2(state.range(0), '-');
for (auto _ : state) {
benchmark::DoNotOptimize(s1.compare(s2));
auto comparison_result = s1.compare(s2);
benchmark::DoNotOptimize(comparison_result);
}
state.SetComplexityN(state.range(0));
}
Expand Down Expand Up @@ -1005,7 +1007,8 @@ static void BM_vector_push_back(benchmark::State& state) {
for (auto _ : state) {
std::vector<int> v;
v.reserve(1);
benchmark::DoNotOptimize(v.data()); // Allow v.data() to be clobbered.
auto data = v.data(); // Allow v.data() to be clobbered. Pass as non-const
benchmark::DoNotOptimize(data); // lvalue to avoid undesired compiler optimizations
v.push_back(42);
benchmark::ClobberMemory(); // Force 42 to be written to memory.
}
Expand Down

0 comments on commit 0111636

Please sign in to comment.