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 //===----------------------------------------------------------------------===//
12 #include "benchmark/benchmark.h"
15 void run_sizes(auto benchmark
) {
30 template <class FromContainer
, class ToContainer
, class Func
>
31 void benchmark_containers(benchmark::State
& state
, FromContainer
& d
, ToContainer
& v
, Func
&& func
) {
32 for (auto _
: state
) {
33 benchmark::DoNotOptimize(v
);
34 benchmark::DoNotOptimize(d
);
35 func(d
.begin(), d
.end(), v
.begin());
40 void benchmark_deque_vector(benchmark::State
& state
, Func
&& func
) {
41 auto size
= state
.range(0);
44 std::ranges::fill(d
, 10);
47 benchmark_containers(state
, d
, v
, func
);
51 void benchmark_deque_deque(benchmark::State
& state
, Func
&& func
) {
52 auto size
= state
.range(0);
55 std::ranges::fill(d
, 10);
58 benchmark_containers(state
, d
, v
, func
);
62 void benchmark_vector_deque(benchmark::State
& state
, Func
&& func
) {
63 auto size
= state
.range(0);
66 std::ranges::fill(d
, 10);
69 benchmark_containers(state
, d
, v
, func
);
72 template <class FromContainer
, class ToContainer
, class Func
>
73 void benchmark_containers_backward(benchmark::State
& state
, FromContainer
& d
, ToContainer
& v
, Func
&& func
) {
74 for (auto _
: state
) {
75 benchmark::DoNotOptimize(v
);
76 benchmark::DoNotOptimize(d
);
77 func(d
.begin(), d
.end(), v
.end());
82 void benchmark_deque_vector_backward(benchmark::State
& state
, Func
&& func
) {
83 auto size
= state
.range(0);
86 std::ranges::fill(d
, 10);
89 benchmark_containers_backward(state
, d
, v
, func
);
93 void benchmark_deque_deque_backward(benchmark::State
& state
, Func
&& func
) {
94 auto size
= state
.range(0);
97 std::ranges::fill(d
, 10);
100 benchmark_containers_backward(state
, d
, v
, func
);
103 template <class Func
>
104 void benchmark_vector_deque_backward(benchmark::State
& state
, Func
&& func
) {
105 auto size
= state
.range(0);
108 std::ranges::fill(d
, 10);
111 benchmark_containers_backward(state
, d
, v
, func
);
115 template <class... Args
>
116 auto operator()(Args
... args
) const {
117 std::copy(std::forward
<Args
>(args
)...);
122 template <class... Args
>
123 auto operator()(Args
... args
) const {
124 std::move(std::forward
<Args
>(args
)...);
128 struct CopyBackwardFunctor
{
129 template <class... Args
>
130 auto operator()(Args
... args
) const {
131 std::copy_backward(std::forward
<Args
>(args
)...);
135 struct MoveBackwardFunctor
{
136 template <class... Args
>
137 auto operator()(Args
... args
) const {
138 std::move_backward(std::forward
<Args
>(args
)...);
143 void BM_deque_vector_copy(benchmark::State
& state
) { benchmark_deque_vector(state
, copy
); }
144 BENCHMARK(BM_deque_vector_copy
)->Apply(run_sizes
);
146 void BM_deque_vector_ranges_copy(benchmark::State
& state
) { benchmark_deque_vector(state
, std::ranges::copy
); }
147 BENCHMARK(BM_deque_vector_ranges_copy
)->Apply(run_sizes
);
149 void BM_deque_deque_copy(benchmark::State
& state
) { benchmark_deque_deque(state
, copy
); }
150 BENCHMARK(BM_deque_deque_copy
)->Apply(run_sizes
);
152 void BM_deque_deque_ranges_copy(benchmark::State
& state
) { benchmark_deque_deque(state
, std::ranges::copy
); }
153 BENCHMARK(BM_deque_deque_ranges_copy
)->Apply(run_sizes
);
155 void BM_vector_deque_copy(benchmark::State
& state
) { benchmark_vector_deque(state
, copy
); }
156 BENCHMARK(BM_vector_deque_copy
)->Apply(run_sizes
);
158 void BM_vector_deque_ranges_copy(benchmark::State
& state
) { benchmark_vector_deque(state
, std::ranges::copy
); }
159 BENCHMARK(BM_vector_deque_ranges_copy
)->Apply(run_sizes
);
162 void BM_deque_vector_move(benchmark::State
& state
) { benchmark_deque_vector(state
, move
); }
163 BENCHMARK(BM_deque_vector_move
)->Apply(run_sizes
);
165 void BM_deque_vector_ranges_move(benchmark::State
& state
) { benchmark_deque_vector(state
, std::ranges::move
); }
166 BENCHMARK(BM_deque_vector_ranges_move
)->Apply(run_sizes
);
168 void BM_deque_deque_move(benchmark::State
& state
) { benchmark_deque_deque(state
, move
); }
169 BENCHMARK(BM_deque_deque_move
)->Apply(run_sizes
);
171 void BM_deque_deque_ranges_move(benchmark::State
& state
) { benchmark_deque_deque(state
, std::ranges::move
); }
172 BENCHMARK(BM_deque_deque_ranges_move
)->Apply(run_sizes
);
174 void BM_vector_deque_move(benchmark::State
& state
) { benchmark_vector_deque(state
, move
); }
175 BENCHMARK(BM_vector_deque_move
)->Apply(run_sizes
);
177 void BM_vector_deque_ranges_move(benchmark::State
& state
) { benchmark_vector_deque(state
, std::ranges::move
); }
178 BENCHMARK(BM_vector_deque_ranges_move
)->Apply(run_sizes
);
181 void BM_deque_vector_copy_backward(benchmark::State
& state
) { benchmark_deque_vector_backward(state
, copy_backward
); }
182 BENCHMARK(BM_deque_vector_copy_backward
)->Apply(run_sizes
);
184 void BM_deque_vector_ranges_copy_backward(benchmark::State
& state
) {
185 benchmark_deque_vector_backward(state
, std::ranges::copy_backward
);
187 BENCHMARK(BM_deque_vector_ranges_copy_backward
)->Apply(run_sizes
);
189 void BM_deque_deque_copy_backward(benchmark::State
& state
) { benchmark_deque_deque_backward(state
, copy_backward
); }
190 BENCHMARK(BM_deque_deque_copy_backward
)->Apply(run_sizes
);
192 void BM_deque_deque_ranges_copy_backward(benchmark::State
& state
) {
193 benchmark_deque_deque_backward(state
, std::ranges::copy_backward
);
195 BENCHMARK(BM_deque_deque_ranges_copy_backward
)->Apply(run_sizes
);
197 void BM_vector_deque_copy_backward(benchmark::State
& state
) { benchmark_vector_deque_backward(state
, copy_backward
); }
198 BENCHMARK(BM_vector_deque_copy_backward
)->Apply(run_sizes
);
200 void BM_vector_deque_ranges_copy_backward(benchmark::State
& state
) {
201 benchmark_vector_deque_backward(state
, std::ranges::copy_backward
);
203 BENCHMARK(BM_vector_deque_ranges_copy_backward
)->Apply(run_sizes
);
206 void BM_deque_vector_move_backward(benchmark::State
& state
) { benchmark_deque_vector_backward(state
, move_backward
); }
207 BENCHMARK(BM_deque_vector_move_backward
)->Apply(run_sizes
);
209 void BM_deque_vector_ranges_move_backward(benchmark::State
& state
) {
210 benchmark_deque_vector_backward(state
, std::ranges::move_backward
);
212 BENCHMARK(BM_deque_vector_ranges_move_backward
)->Apply(run_sizes
);
214 void BM_deque_deque_move_backward(benchmark::State
& state
) { benchmark_deque_deque_backward(state
, move_backward
); }
215 BENCHMARK(BM_deque_deque_move_backward
)->Apply(run_sizes
);
217 void BM_deque_deque_ranges_move_backward(benchmark::State
& state
) {
218 benchmark_deque_deque_backward(state
, std::ranges::move_backward
);
220 BENCHMARK(BM_deque_deque_ranges_move_backward
)->Apply(run_sizes
);
222 void BM_vector_deque_move_backward(benchmark::State
& state
) { benchmark_vector_deque_backward(state
, move_backward
); }
223 BENCHMARK(BM_vector_deque_move_backward
)->Apply(run_sizes
);
225 void BM_vector_deque_ranges_move_backward(benchmark::State
& state
) {
226 benchmark_vector_deque_backward(state
, std::ranges::move_backward
);
228 BENCHMARK(BM_vector_deque_ranges_move_backward
)->Apply(run_sizes
);