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
16template <class Container>
17static 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}
31BENCHMARK(bm_find<std::vector<char>>)->DenseRange(start: 1, limit: 8)->Range(start: 16, limit: 1 << 20);
32BENCHMARK(bm_find<std::vector<short>>)->DenseRange(start: 1, limit: 8)->Range(start: 16, limit: 1 << 20);
33BENCHMARK(bm_find<std::vector<int>>)->DenseRange(start: 1, limit: 8)->Range(start: 16, limit: 1 << 20);
34BENCHMARK(bm_find<std::deque<char>>)->DenseRange(start: 1, limit: 8)->Range(start: 16, limit: 1 << 20);
35BENCHMARK(bm_find<std::deque<short>>)->DenseRange(start: 1, limit: 8)->Range(start: 16, limit: 1 << 20);
36BENCHMARK(bm_find<std::deque<int>>)->DenseRange(start: 1, limit: 8)->Range(start: 16, limit: 1 << 20);
37
38template <class Container>
39static 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}
53BENCHMARK(bm_ranges_find<std::vector<char>>)->DenseRange(start: 1, limit: 8)->Range(start: 16, limit: 1 << 20);
54BENCHMARK(bm_ranges_find<std::vector<short>>)->DenseRange(start: 1, limit: 8)->Range(start: 16, limit: 1 << 20);
55BENCHMARK(bm_ranges_find<std::vector<int>>)->DenseRange(start: 1, limit: 8)->Range(start: 16, limit: 1 << 20);
56BENCHMARK(bm_ranges_find<std::deque<char>>)->DenseRange(start: 1, limit: 8)->Range(start: 16, limit: 1 << 20);
57BENCHMARK(bm_ranges_find<std::deque<short>>)->DenseRange(start: 1, limit: 8)->Range(start: 16, limit: 1 << 20);
58BENCHMARK(bm_ranges_find<std::deque<int>>)->DenseRange(start: 1, limit: 8)->Range(start: 16, limit: 1 << 20);
59
60static 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(value&: vec1);
68 benchmark::DoNotOptimize(value: std::find(first: vec1.begin(), last: vec1.end(), val: true));
69 vec1[idx] = false;
70 }
71}
72BENCHMARK(bm_vector_bool_find)->DenseRange(start: 1, limit: 8)->Range(start: 16, limit: 1 << 20);
73
74static 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(value&: vec1);
82 benchmark::DoNotOptimize(std::ranges::find(vec1, true));
83 vec1[idx] = false;
84 }
85}
86BENCHMARK(bm_vector_bool_ranges_find)->DenseRange(start: 1, limit: 8)->Range(start: 16, limit: 1 << 20);
87
88BENCHMARK_MAIN();
89

source code of libcxx/benchmarks/algorithms/find.bench.cpp