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, c++20
10+ // ADDITIONAL_COMPILE_FLAGS: -O3
11+ #include < algorithm>
12+ #include < deque>
13+ #include < ranges>
14+ #include < vector>
15+
16+ #include " benchmark/benchmark.h"
17+
18+ namespace {
19+
20+ template <size_t N>
21+ void BM_adjacent_full (benchmark::State& state) {
22+ const std::vector<int > inputs (1000000 , 42 );
23+ auto view = inputs | std::views::adjacent<N>;
24+ for (auto _ : state) {
25+ auto it = view.begin ();
26+ benchmark::DoNotOptimize (it);
27+ }
28+ }
29+
30+ BENCHMARK (BM_adjacent_full<2 >);
31+ BENCHMARK (BM_adjacent_full<3 >);
32+ BENCHMARK (BM_adjacent_full<4 >);
33+ BENCHMARK (BM_adjacent_full<5 >);
34+ BENCHMARK (BM_adjacent_full<6 >);
35+ BENCHMARK (BM_adjacent_full<7 >);
36+ BENCHMARK (BM_adjacent_full<8 >);
37+ BENCHMARK (BM_adjacent_full<9 >);
38+ BENCHMARK (BM_adjacent_full<10 >);
39+ BENCHMARK (BM_adjacent_full<100 >);
40+ BENCHMARK (BM_adjacent_full<1000 >);
41+
42+ template <size_t N>
43+ void BM_adjacent_empty (benchmark::State& state) {
44+ const std::vector<int > inputs;
45+ auto view = inputs | std::views::adjacent<N>;
46+ for (auto _ : state) {
47+ auto it = view.begin ();
48+ benchmark::DoNotOptimize (it);
49+ }
50+ }
51+
52+ BENCHMARK (BM_adjacent_empty<2 >);
53+ BENCHMARK (BM_adjacent_empty<3 >);
54+ BENCHMARK (BM_adjacent_empty<4 >);
55+ BENCHMARK (BM_adjacent_empty<5 >);
56+ BENCHMARK (BM_adjacent_empty<6 >);
57+ BENCHMARK (BM_adjacent_empty<7 >);
58+ BENCHMARK (BM_adjacent_empty<8 >);
59+ BENCHMARK (BM_adjacent_empty<9 >);
60+ BENCHMARK (BM_adjacent_empty<10 >);
61+ BENCHMARK (BM_adjacent_empty<100 >);
62+ BENCHMARK (BM_adjacent_empty<1000 >);
63+
64+ } // namespace
65+
66+ BENCHMARK_MAIN ();
0 commit comments