| 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, c++20 |
| 10 | |
| 11 | #include <algorithm> |
| 12 | #include <cstddef> |
| 13 | #include <deque> |
| 14 | #include <iterator> |
| 15 | #include <list> |
| 16 | #include <vector> |
| 17 | |
| 18 | #include <benchmark/benchmark.h> |
| 19 | #include "../../GenerateInput.h" |
| 20 | |
| 21 | int main(int argc, char** argv) { |
| 22 | // Benchmark ranges::contains where we process the whole sequence, which is the |
| 23 | // worst case. |
| 24 | { |
| 25 | auto bm = []<class Container>(std::string name) { |
| 26 | benchmark::RegisterBenchmark( |
| 27 | name, |
| 28 | [](auto& st) { |
| 29 | std::size_t const size = st.range(0); |
| 30 | using ValueType = typename Container::value_type; |
| 31 | ValueType x = Generate<ValueType>::random(); |
| 32 | ValueType y = random_different_from({x}); |
| 33 | Container c(size, x); |
| 34 | auto first = c.begin(); |
| 35 | auto last = c.end(); |
| 36 | |
| 37 | for ([[maybe_unused]] auto _ : st) { |
| 38 | benchmark::DoNotOptimize(c); |
| 39 | benchmark::DoNotOptimize(y); |
| 40 | auto result = std::ranges::contains(first, last, y); |
| 41 | benchmark::DoNotOptimize(result); |
| 42 | } |
| 43 | }) |
| 44 | ->Arg(8) |
| 45 | ->Arg(32) |
| 46 | ->Arg(50) // non power-of-two |
| 47 | ->Arg(8192) |
| 48 | ->Arg(1 << 20); |
| 49 | }; |
| 50 | bm.operator()<std::vector<int>>(name: "rng::contains(vector<int>) (process all)" ); |
| 51 | bm.operator()<std::deque<int>>(name: "rng::contains(deque<int>) (process all)" ); |
| 52 | bm.operator()<std::list<int>>(name: "rng::contains(list<int>) (process all)" ); |
| 53 | } |
| 54 | |
| 55 | benchmark::Initialize(&argc, argv); |
| 56 | benchmark::RunSpecifiedBenchmarks(); |
| 57 | benchmark::Shutdown(); |
| 58 | return 0; |
| 59 | } |
| 60 | |