Skip to content

Commit 13789b4

Browse files
committed
Address comments on shift_{left,right} benchmarks
1 parent ef203af commit 13789b4

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

libcxx/test/benchmarks/algorithms/modifying/shift_left.bench.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <iterator>
1515
#include <list>
1616
#include <string>
17+
#include <utility>
1718
#include <vector>
1819

1920
#include "benchmark/benchmark.h"
@@ -22,7 +23,7 @@
2223
int main(int argc, char** argv) {
2324
auto std_shift_left = [](auto first, auto last, auto n) { return std::shift_left(first, last, n); };
2425

25-
// std::shift_left(normal container)
26+
// Benchmark std::shift_left where we shift exactly one element, which is the worst case.
2627
{
2728
auto bm = []<class Container>(std::string name, auto shift_left) {
2829
benchmark::RegisterBenchmark(
@@ -32,12 +33,19 @@ int main(int argc, char** argv) {
3233
using ValueType = typename Container::value_type;
3334
Container c;
3435
std::generate_n(std::back_inserter(c), size, [] { return Generate<ValueType>::random(); });
36+
std::size_t n = 1;
3537

36-
auto const n = 9 * (size / 10); // shift all but 10% of the range
38+
// To avoid ending up with a fully moved-from range, restore the element that gets
39+
// overwritten by the shift after performing the shift.
40+
auto first_element = c.begin();
41+
auto last_element = std::next(c.begin(), size - 1);
3742

3843
for ([[maybe_unused]] auto _ : st) {
3944
benchmark::DoNotOptimize(c);
40-
auto result = shift_left(c.begin(), c.end(), n);
45+
benchmark::DoNotOptimize(n);
46+
ValueType tmp = *first_element;
47+
auto result = shift_left(c.begin(), c.end(), n);
48+
*last_element = std::move(tmp);
4149
benchmark::DoNotOptimize(result);
4250
}
4351
})

libcxx/test/benchmarks/algorithms/modifying/shift_right.bench.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <iterator>
1515
#include <list>
1616
#include <string>
17+
#include <utility>
1718
#include <vector>
1819

1920
#include "benchmark/benchmark.h"
@@ -22,7 +23,7 @@
2223
int main(int argc, char** argv) {
2324
auto std_shift_right = [](auto first, auto last, auto n) { return std::shift_right(first, last, n); };
2425

25-
// std::shift_right(normal container)
26+
// Benchmark std::shift_right where we shift exactly one element, which is the worst case.
2627
{
2728
auto bm = []<class Container>(std::string name, auto shift_right) {
2829
benchmark::RegisterBenchmark(
@@ -32,12 +33,19 @@ int main(int argc, char** argv) {
3233
using ValueType = typename Container::value_type;
3334
Container c;
3435
std::generate_n(std::back_inserter(c), size, [] { return Generate<ValueType>::random(); });
36+
std::size_t n = 1;
3537

36-
auto const n = 9 * (size / 10); // shift all but 10% of the range
38+
// To avoid ending up with a fully moved-from range, restore the element that gets
39+
// overwritten by the shift after performing the shift.
40+
auto first_element = c.begin();
41+
auto last_element = std::next(c.begin(), size - 1);
3742

3843
for ([[maybe_unused]] auto _ : st) {
3944
benchmark::DoNotOptimize(c);
40-
auto result = shift_right(c.begin(), c.end(), n);
45+
benchmark::DoNotOptimize(n);
46+
ValueType tmp = *last_element;
47+
auto result = shift_right(c.begin(), c.end(), n);
48+
*first_element = std::move(tmp);
4149
benchmark::DoNotOptimize(result);
4250
}
4351
})

0 commit comments

Comments
 (0)