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 <benchmark/benchmark.h> |
11 | #include <cstring> |
12 | #include <deque> |
13 | #include <random> |
14 | #include <vector> |
15 | |
16 | template <class Container> |
17 | static void bm_find(benchmark::State& state) { |
18 | using T = Container::value_type; |
19 | |
20 | Container vec1(state.range(), '1'); |
21 | std::mt19937_64 rng(std::random_device{}()); |
22 | |
23 | for (auto _ : state) { |
24 | auto idx = rng() % vec1.size(); |
25 | vec1[idx] = '2'; |
26 | benchmark::DoNotOptimize(vec1); |
27 | benchmark::DoNotOptimize(std::find(vec1.begin(), vec1.end(), T('2'))); |
28 | vec1[idx] = '1'; |
29 | } |
30 | } |
31 | BENCHMARK(bm_find<std::vector<char>>)->DenseRange(1, 8)->Range(16, 1 << 20); |
32 | BENCHMARK(bm_find<std::vector<short>>)->DenseRange(1, 8)->Range(16, 1 << 20); |
33 | BENCHMARK(bm_find<std::vector<int>>)->DenseRange(1, 8)->Range(16, 1 << 20); |
34 | BENCHMARK(bm_find<std::deque<char>>)->DenseRange(1, 8)->Range(16, 1 << 20); |
35 | BENCHMARK(bm_find<std::deque<short>>)->DenseRange(1, 8)->Range(16, 1 << 20); |
36 | BENCHMARK(bm_find<std::deque<int>>)->DenseRange(1, 8)->Range(16, 1 << 20); |
37 | |
38 | template <class Container> |
39 | static void bm_ranges_find(benchmark::State& state) { |
40 | using T = Container::value_type; |
41 | |
42 | Container vec1(state.range(), '1'); |
43 | std::mt19937_64 rng(std::random_device{}()); |
44 | |
45 | for (auto _ : state) { |
46 | auto idx = rng() % vec1.size(); |
47 | vec1[idx] = '2'; |
48 | benchmark::DoNotOptimize(vec1); |
49 | benchmark::DoNotOptimize(std::ranges::find(vec1, T('2'))); |
50 | vec1[idx] = '1'; |
51 | } |
52 | } |
53 | BENCHMARK(bm_ranges_find<std::vector<char>>)->DenseRange(1, 8)->Range(16, 1 << 20); |
54 | BENCHMARK(bm_ranges_find<std::vector<short>>)->DenseRange(1, 8)->Range(16, 1 << 20); |
55 | BENCHMARK(bm_ranges_find<std::vector<int>>)->DenseRange(1, 8)->Range(16, 1 << 20); |
56 | BENCHMARK(bm_ranges_find<std::deque<char>>)->DenseRange(1, 8)->Range(16, 1 << 20); |
57 | BENCHMARK(bm_ranges_find<std::deque<short>>)->DenseRange(1, 8)->Range(16, 1 << 20); |
58 | BENCHMARK(bm_ranges_find<std::deque<int>>)->DenseRange(1, 8)->Range(16, 1 << 20); |
59 | |
60 | static void bm_vector_bool_find(benchmark::State& state) { |
61 | std::vector<bool> vec1(state.range(), false); |
62 | std::mt19937_64 rng(std::random_device{}()); |
63 | |
64 | for (auto _ : state) { |
65 | auto idx = rng() % vec1.size(); |
66 | vec1[idx] = true; |
67 | benchmark::DoNotOptimize(vec1); |
68 | benchmark::DoNotOptimize(std::find(vec1.begin(), vec1.end(), true)); |
69 | vec1[idx] = false; |
70 | } |
71 | } |
72 | BENCHMARK(bm_vector_bool_find)->DenseRange(1, 8)->Range(16, 1 << 20); |
73 | |
74 | static void bm_vector_bool_ranges_find(benchmark::State& state) { |
75 | std::vector<bool> vec1(state.range(), false); |
76 | std::mt19937_64 rng(std::random_device{}()); |
77 | |
78 | for (auto _ : state) { |
79 | auto idx = rng() % vec1.size(); |
80 | vec1[idx] = true; |
81 | benchmark::DoNotOptimize(vec1); |
82 | benchmark::DoNotOptimize(std::ranges::find(vec1, true)); |
83 | vec1[idx] = false; |
84 | } |
85 | } |
86 | BENCHMARK(bm_vector_bool_ranges_find)->DenseRange(1, 8)->Range(16, 1 << 20); |
87 | |
88 | BENCHMARK_MAIN(); |
89 | |