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 //===----------------------------------------------------------------------===//
10 #include <benchmark/benchmark.h>
16 static void bm_find(benchmark::State
& state
) {
17 std::vector
<T
> vec1(state
.range(), '1');
18 std::mt19937_64
rng(std::random_device
{}());
20 for (auto _
: state
) {
21 auto idx
= rng() % vec1
.size();
23 benchmark::DoNotOptimize(vec1
);
24 benchmark::DoNotOptimize(std::find(vec1
.begin(), vec1
.end(), T('2')));
28 BENCHMARK(bm_find
<char>)->DenseRange(1, 8)->Range(16, 1 << 20);
29 BENCHMARK(bm_find
<short>)->DenseRange(1, 8)->Range(16, 1 << 20);
30 BENCHMARK(bm_find
<int>)->DenseRange(1, 8)->Range(16, 1 << 20);
33 static void bm_ranges_find(benchmark::State
& state
) {
34 std::vector
<T
> vec1(state
.range(), '1');
35 std::mt19937_64
rng(std::random_device
{}());
37 for (auto _
: state
) {
38 auto idx
= rng() % vec1
.size();
40 benchmark::DoNotOptimize(vec1
);
41 benchmark::DoNotOptimize(std::ranges::find(vec1
, T('2')));
45 BENCHMARK(bm_ranges_find
<char>)->DenseRange(1, 8)->Range(16, 1 << 20);
46 BENCHMARK(bm_ranges_find
<short>)->DenseRange(1, 8)->Range(16, 1 << 20);
47 BENCHMARK(bm_ranges_find
<int>)->DenseRange(1, 8)->Range(16, 1 << 20);
49 static void bm_vector_bool_find(benchmark::State
& state
) {
50 std::vector
<bool> vec1(state
.range(), false);
51 std::mt19937_64
rng(std::random_device
{}());
53 for (auto _
: state
) {
54 auto idx
= rng() % vec1
.size();
56 benchmark::DoNotOptimize(vec1
);
57 benchmark::DoNotOptimize(std::find(vec1
.begin(), vec1
.end(), true));
61 BENCHMARK(bm_vector_bool_find
)->DenseRange(1, 8)->Range(16, 1 << 20);
63 static void bm_vector_bool_ranges_find(benchmark::State
& state
) {
64 std::vector
<bool> vec1(state
.range(), false);
65 std::mt19937_64
rng(std::random_device
{}());
67 for (auto _
: state
) {
68 auto idx
= rng() % vec1
.size();
70 benchmark::DoNotOptimize(vec1
);
71 benchmark::DoNotOptimize(std::ranges::find(vec1
, true));
75 BENCHMARK(bm_vector_bool_ranges_find
)->DenseRange(1, 8)->Range(16, 1 << 20);