llvm-project
232 строки · 7.7 Кб
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#include <algorithm>
10#include <deque>
11
12#include "benchmark/benchmark.h"
13
14namespace {
15void run_sizes(auto benchmark) {
16benchmark->Arg(0)
17->Arg(1)
18->Arg(2)
19->Arg(64)
20->Arg(512)
21->Arg(1024)
22->Arg(4000)
23->Arg(4096)
24->Arg(5500)
25->Arg(64000)
26->Arg(65536)
27->Arg(70000);
28}
29
30template <class FromContainer, class ToContainer, class Func>
31void benchmark_containers(benchmark::State& state, FromContainer& d, ToContainer& v, Func&& func) {
32for (auto _ : state) {
33benchmark::DoNotOptimize(v);
34benchmark::DoNotOptimize(d);
35func(d.begin(), d.end(), v.begin());
36}
37}
38
39template <class Func>
40void benchmark_deque_vector(benchmark::State& state, Func&& func) {
41auto size = state.range(0);
42std::deque<int> d;
43d.resize(size);
44std::ranges::fill(d, 10);
45std::vector<int> v;
46v.resize(size);
47benchmark_containers(state, d, v, func);
48}
49
50template <class Func>
51void benchmark_deque_deque(benchmark::State& state, Func&& func) {
52auto size = state.range(0);
53std::deque<int> d;
54d.resize(size);
55std::ranges::fill(d, 10);
56std::deque<int> v;
57v.resize(size);
58benchmark_containers(state, d, v, func);
59}
60
61template <class Func>
62void benchmark_vector_deque(benchmark::State& state, Func&& func) {
63auto size = state.range(0);
64std::vector<int> d;
65d.resize(size);
66std::ranges::fill(d, 10);
67std::deque<int> v;
68v.resize(size);
69benchmark_containers(state, d, v, func);
70}
71
72template <class FromContainer, class ToContainer, class Func>
73void benchmark_containers_backward(benchmark::State& state, FromContainer& d, ToContainer& v, Func&& func) {
74for (auto _ : state) {
75benchmark::DoNotOptimize(v);
76benchmark::DoNotOptimize(d);
77func(d.begin(), d.end(), v.end());
78}
79}
80
81template <class Func>
82void benchmark_deque_vector_backward(benchmark::State& state, Func&& func) {
83auto size = state.range(0);
84std::deque<int> d;
85d.resize(size);
86std::ranges::fill(d, 10);
87std::vector<int> v;
88v.resize(size);
89benchmark_containers_backward(state, d, v, func);
90}
91
92template <class Func>
93void benchmark_deque_deque_backward(benchmark::State& state, Func&& func) {
94auto size = state.range(0);
95std::deque<int> d;
96d.resize(size);
97std::ranges::fill(d, 10);
98std::deque<int> v;
99v.resize(size);
100benchmark_containers_backward(state, d, v, func);
101}
102
103template <class Func>
104void benchmark_vector_deque_backward(benchmark::State& state, Func&& func) {
105auto size = state.range(0);
106std::vector<int> d;
107d.resize(size);
108std::ranges::fill(d, 10);
109std::deque<int> v;
110v.resize(size);
111benchmark_containers_backward(state, d, v, func);
112}
113
114struct CopyFunctor {
115template <class... Args>
116auto operator()(Args... args) const {
117std::copy(std::forward<Args>(args)...);
118}
119} copy;
120
121struct MoveFunctor {
122template <class... Args>
123auto operator()(Args... args) const {
124std::move(std::forward<Args>(args)...);
125}
126} move;
127
128struct CopyBackwardFunctor {
129template <class... Args>
130auto operator()(Args... args) const {
131std::copy_backward(std::forward<Args>(args)...);
132}
133} copy_backward;
134
135struct MoveBackwardFunctor {
136template <class... Args>
137auto operator()(Args... args) const {
138std::move_backward(std::forward<Args>(args)...);
139}
140} move_backward;
141
142// copy
143void BM_deque_vector_copy(benchmark::State& state) { benchmark_deque_vector(state, copy); }
144BENCHMARK(BM_deque_vector_copy)->Apply(run_sizes);
145
146void BM_deque_vector_ranges_copy(benchmark::State& state) { benchmark_deque_vector(state, std::ranges::copy); }
147BENCHMARK(BM_deque_vector_ranges_copy)->Apply(run_sizes);
148
149void BM_deque_deque_copy(benchmark::State& state) { benchmark_deque_deque(state, copy); }
150BENCHMARK(BM_deque_deque_copy)->Apply(run_sizes);
151
152void BM_deque_deque_ranges_copy(benchmark::State& state) { benchmark_deque_deque(state, std::ranges::copy); }
153BENCHMARK(BM_deque_deque_ranges_copy)->Apply(run_sizes);
154
155void BM_vector_deque_copy(benchmark::State& state) { benchmark_vector_deque(state, copy); }
156BENCHMARK(BM_vector_deque_copy)->Apply(run_sizes);
157
158void BM_vector_deque_ranges_copy(benchmark::State& state) { benchmark_vector_deque(state, std::ranges::copy); }
159BENCHMARK(BM_vector_deque_ranges_copy)->Apply(run_sizes);
160
161// move
162void BM_deque_vector_move(benchmark::State& state) { benchmark_deque_vector(state, move); }
163BENCHMARK(BM_deque_vector_move)->Apply(run_sizes);
164
165void BM_deque_vector_ranges_move(benchmark::State& state) { benchmark_deque_vector(state, std::ranges::move); }
166BENCHMARK(BM_deque_vector_ranges_move)->Apply(run_sizes);
167
168void BM_deque_deque_move(benchmark::State& state) { benchmark_deque_deque(state, move); }
169BENCHMARK(BM_deque_deque_move)->Apply(run_sizes);
170
171void BM_deque_deque_ranges_move(benchmark::State& state) { benchmark_deque_deque(state, std::ranges::move); }
172BENCHMARK(BM_deque_deque_ranges_move)->Apply(run_sizes);
173
174void BM_vector_deque_move(benchmark::State& state) { benchmark_vector_deque(state, move); }
175BENCHMARK(BM_vector_deque_move)->Apply(run_sizes);
176
177void BM_vector_deque_ranges_move(benchmark::State& state) { benchmark_vector_deque(state, std::ranges::move); }
178BENCHMARK(BM_vector_deque_ranges_move)->Apply(run_sizes);
179
180// copy_backward
181void BM_deque_vector_copy_backward(benchmark::State& state) { benchmark_deque_vector_backward(state, copy_backward); }
182BENCHMARK(BM_deque_vector_copy_backward)->Apply(run_sizes);
183
184void BM_deque_vector_ranges_copy_backward(benchmark::State& state) {
185benchmark_deque_vector_backward(state, std::ranges::copy_backward);
186}
187BENCHMARK(BM_deque_vector_ranges_copy_backward)->Apply(run_sizes);
188
189void BM_deque_deque_copy_backward(benchmark::State& state) { benchmark_deque_deque_backward(state, copy_backward); }
190BENCHMARK(BM_deque_deque_copy_backward)->Apply(run_sizes);
191
192void BM_deque_deque_ranges_copy_backward(benchmark::State& state) {
193benchmark_deque_deque_backward(state, std::ranges::copy_backward);
194}
195BENCHMARK(BM_deque_deque_ranges_copy_backward)->Apply(run_sizes);
196
197void BM_vector_deque_copy_backward(benchmark::State& state) { benchmark_vector_deque_backward(state, copy_backward); }
198BENCHMARK(BM_vector_deque_copy_backward)->Apply(run_sizes);
199
200void BM_vector_deque_ranges_copy_backward(benchmark::State& state) {
201benchmark_vector_deque_backward(state, std::ranges::copy_backward);
202}
203BENCHMARK(BM_vector_deque_ranges_copy_backward)->Apply(run_sizes);
204
205// move_backward
206void BM_deque_vector_move_backward(benchmark::State& state) { benchmark_deque_vector_backward(state, move_backward); }
207BENCHMARK(BM_deque_vector_move_backward)->Apply(run_sizes);
208
209void BM_deque_vector_ranges_move_backward(benchmark::State& state) {
210benchmark_deque_vector_backward(state, std::ranges::move_backward);
211}
212BENCHMARK(BM_deque_vector_ranges_move_backward)->Apply(run_sizes);
213
214void BM_deque_deque_move_backward(benchmark::State& state) { benchmark_deque_deque_backward(state, move_backward); }
215BENCHMARK(BM_deque_deque_move_backward)->Apply(run_sizes);
216
217void BM_deque_deque_ranges_move_backward(benchmark::State& state) {
218benchmark_deque_deque_backward(state, std::ranges::move_backward);
219}
220BENCHMARK(BM_deque_deque_ranges_move_backward)->Apply(run_sizes);
221
222void BM_vector_deque_move_backward(benchmark::State& state) { benchmark_vector_deque_backward(state, move_backward); }
223BENCHMARK(BM_vector_deque_move_backward)->Apply(run_sizes);
224
225void BM_vector_deque_ranges_move_backward(benchmark::State& state) {
226benchmark_vector_deque_backward(state, std::ranges::move_backward);
227}
228BENCHMARK(BM_vector_deque_ranges_move_backward)->Apply(run_sizes);
229
230} // namespace
231
232BENCHMARK_MAIN();
233