| 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 <cstddef> |
| 13 | #include <deque> |
| 14 | #include <list> |
| 15 | #include <numeric> |
| 16 | #include <string> |
| 17 | #include <vector> |
| 18 | |
| 19 | #include <benchmark/benchmark.h> |
| 20 | #include "../../GenerateInput.h" |
| 21 | |
| 22 | int main(int argc, char** argv) { |
| 23 | auto std_any_of = [](auto first, auto last, auto pred) { return std::any_of(first, last, pred); }; |
| 24 | auto std_all_of = [](auto first, auto last, auto pred) { |
| 25 | // match semantics of any_of |
| 26 | return !std::all_of(first, last, [pred](auto x) { return !pred(x); }); |
| 27 | }; |
| 28 | auto std_none_of = [](auto first, auto last, auto pred) { |
| 29 | // match semantics of any_of |
| 30 | return !std::none_of(first, last, pred); |
| 31 | }; |
| 32 | |
| 33 | auto ranges_all_of = [](auto first, auto last, auto pred) { |
| 34 | // match semantics of any_of |
| 35 | return !std::ranges::all_of(first, last, [pred](auto x) { return !pred(x); }); |
| 36 | }; |
| 37 | auto ranges_none_of = [](auto first, auto last, auto pred) { |
| 38 | // match semantics of any_of |
| 39 | return !std::ranges::none_of(first, last, pred); |
| 40 | }; |
| 41 | |
| 42 | // Benchmark {std,ranges}::{any_of,all_of,none_of} where we process the whole sequence, |
| 43 | // which is the worst case. |
| 44 | { |
| 45 | auto bm = []<class Container>(std::string name, auto any_of) { |
| 46 | benchmark::RegisterBenchmark( |
| 47 | name, |
| 48 | [any_of](auto& st) { |
| 49 | std::size_t const size = st.range(0); |
| 50 | using ValueType = typename Container::value_type; |
| 51 | ValueType x = Generate<ValueType>::random(); |
| 52 | ValueType y = random_different_from({x}); |
| 53 | Container c(size, x); |
| 54 | |
| 55 | for ([[maybe_unused]] auto _ : st) { |
| 56 | benchmark::DoNotOptimize(c); |
| 57 | auto result = any_of(c.begin(), c.end(), [&](auto element) { |
| 58 | benchmark::DoNotOptimize(element); |
| 59 | return element == y; |
| 60 | }); |
| 61 | benchmark::DoNotOptimize(result); |
| 62 | } |
| 63 | }) |
| 64 | ->Arg(8) |
| 65 | ->Arg(32) |
| 66 | ->Arg(50) // non power-of-two |
| 67 | ->Arg(8192) |
| 68 | ->Arg(32768); |
| 69 | }; |
| 70 | |
| 71 | // any_of |
| 72 | bm.operator()<std::vector<int>>(name: "std::any_of(vector<int>) (process all)" , any_of: std_any_of); |
| 73 | bm.operator()<std::deque<int>>(name: "std::any_of(deque<int>) (process all)" , any_of: std_any_of); |
| 74 | bm.operator()<std::list<int>>(name: "std::any_of(list<int>) (process all)" , any_of: std_any_of); |
| 75 | bm.operator()<std::vector<int>>("rng::any_of(vector<int>) (process all)" , std::ranges::any_of); |
| 76 | bm.operator()<std::deque<int>>("rng::any_of(deque<int>) (process all)" , std::ranges::any_of); |
| 77 | bm.operator()<std::list<int>>("rng::any_of(list<int>) (process all)" , std::ranges::any_of); |
| 78 | |
| 79 | // all_of |
| 80 | bm.operator()<std::vector<int>>(name: "std::all_of(vector<int>) (process all)" , any_of: std_all_of); |
| 81 | bm.operator()<std::deque<int>>(name: "std::all_of(deque<int>) (process all)" , any_of: std_all_of); |
| 82 | bm.operator()<std::list<int>>(name: "std::all_of(list<int>) (process all)" , any_of: std_all_of); |
| 83 | bm.operator()<std::vector<int>>(name: "rng::all_of(vector<int>) (process all)" , any_of: ranges_all_of); |
| 84 | bm.operator()<std::deque<int>>(name: "rng::all_of(deque<int>) (process all)" , any_of: ranges_all_of); |
| 85 | bm.operator()<std::list<int>>(name: "rng::all_of(list<int>) (process all)" , any_of: ranges_all_of); |
| 86 | |
| 87 | // none_of |
| 88 | bm.operator()<std::vector<int>>(name: "std::none_of(vector<int>) (process all)" , any_of: std_none_of); |
| 89 | bm.operator()<std::deque<int>>(name: "std::none_of(deque<int>) (process all)" , any_of: std_none_of); |
| 90 | bm.operator()<std::list<int>>(name: "std::none_of(list<int>) (process all)" , any_of: std_none_of); |
| 91 | bm.operator()<std::vector<int>>(name: "rng::none_of(vector<int>) (process all)" , any_of: ranges_none_of); |
| 92 | bm.operator()<std::deque<int>>(name: "rng::none_of(deque<int>) (process all)" , any_of: ranges_none_of); |
| 93 | bm.operator()<std::list<int>>(name: "rng::none_of(list<int>) (process all)" , any_of: ranges_none_of); |
| 94 | } |
| 95 | |
| 96 | benchmark::Initialize(&argc, argv); |
| 97 | benchmark::RunSpecifiedBenchmarks(); |
| 98 | benchmark::Shutdown(); |
| 99 | return 0; |
| 100 | } |
| 101 | |