1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14, c++17
14 #include "benchmark/benchmark.h"
17 void run_sizes(auto benchmark
) {
32 template <class FromContainer
, class ToContainer
, class Func
>
33 void benchmark_containers(benchmark::State
& state
, FromContainer
& d
, ToContainer
& v
, Func
&& func
) {
34 for (auto _
: state
) {
35 benchmark::DoNotOptimize(v
);
36 benchmark::DoNotOptimize(d
);
37 func(d
.begin(), d
.end(), v
.begin());
42 void benchmark_deque_vector(benchmark::State
& state
, Func
&& func
) {
43 auto size
= state
.range(0);
46 std::ranges::fill(d
, 10);
49 benchmark_containers(state
, d
, v
, func
);
53 void benchmark_deque_deque(benchmark::State
& state
, Func
&& func
) {
54 auto size
= state
.range(0);
57 std::ranges::fill(d
, 10);
60 benchmark_containers(state
, d
, v
, func
);
64 void benchmark_vector_deque(benchmark::State
& state
, Func
&& func
) {
65 auto size
= state
.range(0);
68 std::ranges::fill(d
, 10);
71 benchmark_containers(state
, d
, v
, func
);
74 template <class FromContainer
, class ToContainer
, class Func
>
75 void benchmark_containers_backward(benchmark::State
& state
, FromContainer
& d
, ToContainer
& v
, Func
&& func
) {
76 for (auto _
: state
) {
77 benchmark::DoNotOptimize(v
);
78 benchmark::DoNotOptimize(d
);
79 func(d
.begin(), d
.end(), v
.end());
84 void benchmark_deque_vector_backward(benchmark::State
& state
, Func
&& func
) {
85 auto size
= state
.range(0);
88 std::ranges::fill(d
, 10);
91 benchmark_containers_backward(state
, d
, v
, func
);
95 void benchmark_deque_deque_backward(benchmark::State
& state
, Func
&& func
) {
96 auto size
= state
.range(0);
99 std::ranges::fill(d
, 10);
102 benchmark_containers_backward(state
, d
, v
, func
);
105 template <class Func
>
106 void benchmark_vector_deque_backward(benchmark::State
& state
, Func
&& func
) {
107 auto size
= state
.range(0);
110 std::ranges::fill(d
, 10);
113 benchmark_containers_backward(state
, d
, v
, func
);
117 template <class... Args
>
118 auto operator()(Args
... args
) const {
119 std::copy(std::forward
<Args
>(args
)...);
124 template <class... Args
>
125 auto operator()(Args
... args
) const {
126 std::move(std::forward
<Args
>(args
)...);
130 struct CopyBackwardFunctor
{
131 template <class... Args
>
132 auto operator()(Args
... args
) const {
133 std::copy_backward(std::forward
<Args
>(args
)...);
137 struct MoveBackwardFunctor
{
138 template <class... Args
>
139 auto operator()(Args
... args
) const {
140 std::move_backward(std::forward
<Args
>(args
)...);
145 void BM_deque_vector_copy(benchmark::State
& state
) { benchmark_deque_vector(state
, copy
); }
146 BENCHMARK(BM_deque_vector_copy
)->Apply(run_sizes
);
148 void BM_deque_vector_ranges_copy(benchmark::State
& state
) { benchmark_deque_vector(state
, std::ranges::copy
); }
149 BENCHMARK(BM_deque_vector_ranges_copy
)->Apply(run_sizes
);
151 void BM_deque_deque_copy(benchmark::State
& state
) { benchmark_deque_deque(state
, copy
); }
152 BENCHMARK(BM_deque_deque_copy
)->Apply(run_sizes
);
154 void BM_deque_deque_ranges_copy(benchmark::State
& state
) { benchmark_deque_deque(state
, std::ranges::copy
); }
155 BENCHMARK(BM_deque_deque_ranges_copy
)->Apply(run_sizes
);
157 void BM_vector_deque_copy(benchmark::State
& state
) { benchmark_vector_deque(state
, copy
); }
158 BENCHMARK(BM_vector_deque_copy
)->Apply(run_sizes
);
160 void BM_vector_deque_ranges_copy(benchmark::State
& state
) { benchmark_vector_deque(state
, std::ranges::copy
); }
161 BENCHMARK(BM_vector_deque_ranges_copy
)->Apply(run_sizes
);
164 void BM_deque_vector_move(benchmark::State
& state
) { benchmark_deque_vector(state
, move
); }
165 BENCHMARK(BM_deque_vector_move
)->Apply(run_sizes
);
167 void BM_deque_vector_ranges_move(benchmark::State
& state
) { benchmark_deque_vector(state
, std::ranges::move
); }
168 BENCHMARK(BM_deque_vector_ranges_move
)->Apply(run_sizes
);
170 void BM_deque_deque_move(benchmark::State
& state
) { benchmark_deque_deque(state
, move
); }
171 BENCHMARK(BM_deque_deque_move
)->Apply(run_sizes
);
173 void BM_deque_deque_ranges_move(benchmark::State
& state
) { benchmark_deque_deque(state
, std::ranges::move
); }
174 BENCHMARK(BM_deque_deque_ranges_move
)->Apply(run_sizes
);
176 void BM_vector_deque_move(benchmark::State
& state
) { benchmark_vector_deque(state
, move
); }
177 BENCHMARK(BM_vector_deque_move
)->Apply(run_sizes
);
179 void BM_vector_deque_ranges_move(benchmark::State
& state
) { benchmark_vector_deque(state
, std::ranges::move
); }
180 BENCHMARK(BM_vector_deque_ranges_move
)->Apply(run_sizes
);
183 void BM_deque_vector_copy_backward(benchmark::State
& state
) { benchmark_deque_vector_backward(state
, copy_backward
); }
184 BENCHMARK(BM_deque_vector_copy_backward
)->Apply(run_sizes
);
186 void BM_deque_vector_ranges_copy_backward(benchmark::State
& state
) {
187 benchmark_deque_vector_backward(state
, std::ranges::copy_backward
);
189 BENCHMARK(BM_deque_vector_ranges_copy_backward
)->Apply(run_sizes
);
191 void BM_deque_deque_copy_backward(benchmark::State
& state
) { benchmark_deque_deque_backward(state
, copy_backward
); }
192 BENCHMARK(BM_deque_deque_copy_backward
)->Apply(run_sizes
);
194 void BM_deque_deque_ranges_copy_backward(benchmark::State
& state
) {
195 benchmark_deque_deque_backward(state
, std::ranges::copy_backward
);
197 BENCHMARK(BM_deque_deque_ranges_copy_backward
)->Apply(run_sizes
);
199 void BM_vector_deque_copy_backward(benchmark::State
& state
) { benchmark_vector_deque_backward(state
, copy_backward
); }
200 BENCHMARK(BM_vector_deque_copy_backward
)->Apply(run_sizes
);
202 void BM_vector_deque_ranges_copy_backward(benchmark::State
& state
) {
203 benchmark_vector_deque_backward(state
, std::ranges::copy_backward
);
205 BENCHMARK(BM_vector_deque_ranges_copy_backward
)->Apply(run_sizes
);
208 void BM_deque_vector_move_backward(benchmark::State
& state
) { benchmark_deque_vector_backward(state
, move_backward
); }
209 BENCHMARK(BM_deque_vector_move_backward
)->Apply(run_sizes
);
211 void BM_deque_vector_ranges_move_backward(benchmark::State
& state
) {
212 benchmark_deque_vector_backward(state
, std::ranges::move_backward
);
214 BENCHMARK(BM_deque_vector_ranges_move_backward
)->Apply(run_sizes
);
216 void BM_deque_deque_move_backward(benchmark::State
& state
) { benchmark_deque_deque_backward(state
, move_backward
); }
217 BENCHMARK(BM_deque_deque_move_backward
)->Apply(run_sizes
);
219 void BM_deque_deque_ranges_move_backward(benchmark::State
& state
) {
220 benchmark_deque_deque_backward(state
, std::ranges::move_backward
);
222 BENCHMARK(BM_deque_deque_ranges_move_backward
)->Apply(run_sizes
);
224 void BM_vector_deque_move_backward(benchmark::State
& state
) { benchmark_vector_deque_backward(state
, move_backward
); }
225 BENCHMARK(BM_vector_deque_move_backward
)->Apply(run_sizes
);
227 void BM_vector_deque_ranges_move_backward(benchmark::State
& state
) {
228 benchmark_vector_deque_backward(state
, std::ranges::move_backward
);
230 BENCHMARK(BM_vector_deque_ranges_move_backward
)->Apply(run_sizes
);