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
10
11#include <algorithm>
12#include <cassert>
13#include <cstddef>
14#include <deque>
15#include <list>
16#include <string>
17#include <vector>
18
19#include <benchmark/benchmark.h>
20#include "../../GenerateInput.h"
21
22int main(int argc, char** argv) {
23 auto std_search_n = [](auto first, auto last, auto n, auto const& value) {
24 return std::search_n(first, last, n, value);
25 };
26 auto std_search_n_pred = [](auto first, auto last, auto n, auto const& value) {
27 return std::search_n(first, last, n, value, [](auto x, auto y) {
28 benchmark::DoNotOptimize(x);
29 benchmark::DoNotOptimize(y);
30 return x == y;
31 });
32 };
33 auto ranges_search_n_pred = [](auto first, auto last, auto n, auto const& value) {
34 return std::ranges::search_n(first, last, n, value, [](auto x, auto y) {
35 benchmark::DoNotOptimize(x);
36 benchmark::DoNotOptimize(y);
37 return x == y;
38 });
39 };
40
41 // Benchmark {std,ranges}::search_n where the needle is never found (worst case).
42 {
43 auto bm = []<class Container>(std::string name, auto search_n) {
44 benchmark::RegisterBenchmark(
45 name,
46 [search_n](auto& st) {
47 std::size_t const size = st.range(0);
48 using ValueType = typename Container::value_type;
49 ValueType x = Generate<ValueType>::random();
50 ValueType y = random_different_from({x});
51 Container haystack(size, x);
52 std::size_t n = size / 10; // needle size is 10% of the haystack
53
54 for ([[maybe_unused]] auto _ : st) {
55 benchmark::DoNotOptimize(haystack);
56 benchmark::DoNotOptimize(n);
57 benchmark::DoNotOptimize(y);
58 auto result = search_n(haystack.begin(), haystack.end(), n, y);
59 benchmark::DoNotOptimize(result);
60 }
61 })
62 ->Arg(1000) // non power-of-two
63 ->Arg(1024)
64 ->Arg(8192)
65 ->Arg(1 << 20);
66 };
67 // {std,ranges}::search_n
68 bm.operator()<std::vector<int>>(name: "std::search_n(vector<int>) (no match)", search_n: std_search_n);
69 bm.operator()<std::deque<int>>(name: "std::search_n(deque<int>) (no match)", search_n: std_search_n);
70 bm.operator()<std::list<int>>(name: "std::search_n(list<int>) (no match)", search_n: std_search_n);
71 bm.operator()<std::vector<int>>("rng::search_n(vector<int>) (no match)", std::ranges::search_n);
72 bm.operator()<std::deque<int>>("rng::search_n(deque<int>) (no match)", std::ranges::search_n);
73 bm.operator()<std::list<int>>("rng::search_n(list<int>) (no match)", std::ranges::search_n);
74
75 // {std,ranges}::search_n(pred)
76 bm.operator()<std::vector<int>>(name: "std::search_n(vector<int>, pred) (no match)", search_n: std_search_n_pred);
77 bm.operator()<std::deque<int>>(name: "std::search_n(deque<int>, pred) (no match)", search_n: std_search_n_pred);
78 bm.operator()<std::list<int>>(name: "std::search_n(list<int>, pred) (no match)", search_n: std_search_n_pred);
79 bm.operator()<std::vector<int>>(name: "rng::search_n(vector<int>, pred) (no match)", search_n: ranges_search_n_pred);
80 bm.operator()<std::deque<int>>(name: "rng::search_n(deque<int>, pred) (no match)", search_n: ranges_search_n_pred);
81 bm.operator()<std::list<int>>(name: "rng::search_n(list<int>, pred) (no match)", search_n: ranges_search_n_pred);
82 }
83
84 benchmark::Initialize(&argc, argv);
85 benchmark::RunSpecifiedBenchmarks();
86 benchmark::Shutdown();
87 return 0;
88}
89

source code of libcxx/test/benchmarks/algorithms/nonmodifying/search_n.bench.cpp