| 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 <iterator> |
| 15 | #include <list> |
| 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_is_sorted_until = [](auto first, auto last) { return std::is_sorted_until(first, last); }; |
| 24 | auto std_is_sorted_until_pred = [](auto first, auto last) { |
| 25 | return std::is_sorted_until(first, last, [](auto x, auto y) { |
| 26 | benchmark::DoNotOptimize(x); |
| 27 | benchmark::DoNotOptimize(y); |
| 28 | return x < y; |
| 29 | }); |
| 30 | }; |
| 31 | auto ranges_is_sorted_until_pred = [](auto first, auto last) { |
| 32 | return std::ranges::is_sorted_until(first, last, [](auto x, auto y) { |
| 33 | benchmark::DoNotOptimize(x); |
| 34 | benchmark::DoNotOptimize(y); |
| 35 | return x < y; |
| 36 | }); |
| 37 | }; |
| 38 | |
| 39 | // Benchmark {std,ranges}::is_sorted_until on a sorted sequence (the worst case). |
| 40 | { |
| 41 | auto bm = []<class Container>(std::string name, auto is_sorted_until) { |
| 42 | benchmark::RegisterBenchmark( |
| 43 | name, |
| 44 | [is_sorted_until](auto& st) { |
| 45 | std::size_t const size = st.range(0); |
| 46 | using ValueType = typename Container::value_type; |
| 47 | std::vector<ValueType> data; |
| 48 | std::generate_n(std::back_inserter(data), size, [] { return Generate<ValueType>::random(); }); |
| 49 | std::sort(data.begin(), data.end()); |
| 50 | |
| 51 | Container c(data.begin(), data.end()); |
| 52 | |
| 53 | for ([[maybe_unused]] auto _ : st) { |
| 54 | benchmark::DoNotOptimize(c); |
| 55 | auto result = is_sorted_until(c.begin(), c.end()); |
| 56 | benchmark::DoNotOptimize(result); |
| 57 | } |
| 58 | }) |
| 59 | ->Arg(8) |
| 60 | ->Arg(1024) |
| 61 | ->Arg(8192); |
| 62 | }; |
| 63 | bm.operator()<std::vector<int>>(name: "std::is_sorted_until(vector<int>)" , is_sorted_until: std_is_sorted_until); |
| 64 | bm.operator()<std::deque<int>>(name: "std::is_sorted_until(deque<int>)" , is_sorted_until: std_is_sorted_until); |
| 65 | bm.operator()<std::list<int>>(name: "std::is_sorted_until(list<int>)" , is_sorted_until: std_is_sorted_until); |
| 66 | bm.operator()<std::vector<int>>("rng::is_sorted_until(vector<int>)" , std::ranges::is_sorted_until); |
| 67 | bm.operator()<std::deque<int>>("rng::is_sorted_until(deque<int>)" , std::ranges::is_sorted_until); |
| 68 | bm.operator()<std::list<int>>("rng::is_sorted_until(list<int>)" , std::ranges::is_sorted_until); |
| 69 | |
| 70 | bm.operator()<std::vector<int>>(name: "std::is_sorted_until(vector<int>, pred)" , is_sorted_until: std_is_sorted_until_pred); |
| 71 | bm.operator()<std::deque<int>>(name: "std::is_sorted_until(deque<int>, pred)" , is_sorted_until: std_is_sorted_until_pred); |
| 72 | bm.operator()<std::list<int>>(name: "std::is_sorted_until(list<int>, pred)" , is_sorted_until: std_is_sorted_until_pred); |
| 73 | bm.operator()<std::vector<int>>(name: "rng::is_sorted_until(vector<int>, pred)" , is_sorted_until: ranges_is_sorted_until_pred); |
| 74 | bm.operator()<std::deque<int>>(name: "rng::is_sorted_until(deque<int>, pred)" , is_sorted_until: ranges_is_sorted_until_pred); |
| 75 | bm.operator()<std::list<int>>(name: "rng::is_sorted_until(list<int>, pred)" , is_sorted_until: ranges_is_sorted_until_pred); |
| 76 | } |
| 77 | |
| 78 | benchmark::Initialize(&argc, argv); |
| 79 | benchmark::RunSpecifiedBenchmarks(); |
| 80 | benchmark::Shutdown(); |
| 81 | return 0; |
| 82 | } |
| 83 | |