1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9// UNSUPPORTED: c++03, c++11, c++14, c++17
10
11#include <algorithm>
12#include <cstddef>
13#include <deque>
14#include <iterator>
15#include <list>
16#include <string>
17#include <vector>
18
19#include "benchmark/benchmark.h"
20#include "../../GenerateInput.h"
21#include "test_macros.h"
22
23int main(int argc, char** argv) {
24 auto std_move_backward = [](auto first, auto last, auto out) { return std::move_backward(first, last, out); };
25
26 // {std,ranges}::move_backward(normal container)
27 {
28 auto bm = []<class Container>(std::string name, auto move_backward) {
29 benchmark::RegisterBenchmark(name, [move_backward](auto& st) {
30 std::size_t const size = st.range(0);
31 using ValueType = typename Container::value_type;
32 Container c1(size);
33 Container c2(size);
34 std::generate_n(c1.begin(), size, [] { return Generate<ValueType>::random(); });
35
36 Container* in = &c1;
37 Container* out = &c2;
38 for ([[maybe_unused]] auto _ : st) {
39 benchmark::DoNotOptimize(in);
40 benchmark::DoNotOptimize(out);
41 auto result = move_backward(in->begin(), in->end(), out->end());
42 benchmark::DoNotOptimize(result);
43 std::swap(in, out);
44 }
45 })->Range(8, 1 << 20);
46 };
47 bm.operator()<std::vector<int>>("std::move_backward(vector<int>)", std_move_backward);
48 bm.operator()<std::deque<int>>("std::move_backward(deque<int>)", std_move_backward);
49 bm.operator()<std::list<int>>("std::move_backward(list<int>)", std_move_backward);
50 bm.operator()<std::vector<int>>("rng::move_backward(vector<int>)", std::ranges::move_backward);
51 bm.operator()<std::deque<int>>("rng::move_backward(deque<int>)", std::ranges::move_backward);
52 bm.operator()<std::list<int>>("rng::move_backward(list<int>)", std::ranges::move_backward);
53 }
54
55 // {std,ranges}::move_backward(vector<bool>)
56 {
57 auto bm = []<bool Aligned>(std::string name, auto move_backward) {
58 benchmark::RegisterBenchmark(name, [move_backward](auto& st) {
59 std::size_t const size = st.range(0);
60 std::vector<bool> c1(size, true);
61 std::vector<bool> c2(size, false);
62
63 std::vector<bool>* in = &c1;
64 std::vector<bool>* out = &c2;
65 for ([[maybe_unused]] auto _ : st) {
66 benchmark::DoNotOptimize(in);
67 benchmark::DoNotOptimize(out);
68 auto last = Aligned ? in->end() : in->end() - 4;
69 auto result = move_backward(in->begin(), last, out->end());
70 benchmark::DoNotOptimize(result);
71 std::swap(in, out);
72 }
73 })->Range(64, 1 << 20);
74 };
75 bm.operator()<true>("std::move_backward(vector<bool>) (aligned)", std_move_backward);
76 bm.operator()<false>("std::move_backward(vector<bool>) (unaligned)", std_move_backward);
77#if TEST_STD_VER >= 23 // vector<bool>::iterator is not an output_iterator before C++23
78 bm.operator()<true>("rng::move_backward(vector<bool>) (aligned)", std::ranges::move_backward);
79 bm.operator()<false>("rng::move_backward(vector<bool>) (unaligned)", std::ranges::move_backward);
80#endif
81 }
82
83 benchmark::Initialize(&argc, argv);
84 benchmark::RunSpecifiedBenchmarks();
85 benchmark::Shutdown();
86 return 0;
87}
88

source code of libcxx/test/benchmarks/algorithms/modifying/move_backward.bench.cpp