Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / benchmarks / algorithms / find.bench.cpp
blobb87c575a16b4dcdc8f0d7e0b40407aa0ef10e1c3
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 //===----------------------------------------------------------------------===//
9 #include <algorithm>
10 #include <benchmark/benchmark.h>
11 #include <cstring>
12 #include <random>
13 #include <vector>
15 template <class T>
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();
22 vec1[idx] = '2';
23 benchmark::DoNotOptimize(vec1);
24 benchmark::DoNotOptimize(std::find(vec1.begin(), vec1.end(), T('2')));
25 vec1[idx] = '1';
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);
32 template <class T>
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();
39 vec1[idx] = '2';
40 benchmark::DoNotOptimize(vec1);
41 benchmark::DoNotOptimize(std::ranges::find(vec1, T('2')));
42 vec1[idx] = '1';
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();
55 vec1[idx] = true;
56 benchmark::DoNotOptimize(vec1);
57 benchmark::DoNotOptimize(std::find(vec1.begin(), vec1.end(), true));
58 vec1[idx] = false;
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();
69 vec1[idx] = true;
70 benchmark::DoNotOptimize(vec1);
71 benchmark::DoNotOptimize(std::ranges::find(vec1, true));
72 vec1[idx] = false;
75 BENCHMARK(bm_vector_bool_ranges_find)->DenseRange(1, 8)->Range(16, 1 << 20);
77 BENCHMARK_MAIN();