brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.6 KiB · 2950ad5 Raw
71 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// UNSUPPORTED: c++03, c++11, c++14, c++17, c++2010 11#include <algorithm>12#include <cstddef>13#include <deque>14#include <list>15#include <string>16#include <vector>17 18#include <benchmark/benchmark.h>19#include "../../GenerateInput.h"20 21int main(int argc, char** argv) {22  auto ranges_starts_with_pred = [](auto first1, auto last1, auto first2, auto last2) {23    return std::ranges::starts_with(first1, last1, first2, last2, [](auto x, auto y) {24      benchmark::DoNotOptimize(x);25      benchmark::DoNotOptimize(y);26      return x == y;27    });28  };29 30  // Benchmark ranges::starts_with where we find the mismatching element at the very end (worst case).31  {32    auto bm = []<class Container>(std::string name, auto starts_with) {33      benchmark::RegisterBenchmark(34          name,35          [starts_with](auto& st) {36            std::size_t const size = st.range(0);37            using ValueType        = typename Container::value_type;38            ValueType x            = Generate<ValueType>::random();39            ValueType y            = random_different_from({x});40            Container c1(size, x);41            Container c2(size, x);42            c2.back() = y;43 44            for ([[maybe_unused]] auto _ : st) {45              benchmark::DoNotOptimize(c1);46              benchmark::DoNotOptimize(c2);47              auto result = starts_with(c1.begin(), c1.end(), c2.begin(), c2.end());48              benchmark::DoNotOptimize(result);49            }50          })51          ->Arg(8)52          ->Arg(1000) // non power-of-two53          ->Arg(1024)54          ->Arg(8192)55          ->Arg(1 << 20);56    };57    bm.operator()<std::vector<int>>("rng::starts_with(vector<int>)", std::ranges::starts_with);58    bm.operator()<std::deque<int>>("rng::starts_with(deque<int>)", std::ranges::starts_with);59    bm.operator()<std::list<int>>("rng::starts_with(list<int>)", std::ranges::starts_with);60 61    bm.operator()<std::vector<int>>("rng::starts_with(vector<int>, pred)", ranges_starts_with_pred);62    bm.operator()<std::deque<int>>("rng::starts_with(deque<int>, pred)", ranges_starts_with_pred);63    bm.operator()<std::list<int>>("rng::starts_with(list<int>, pred)", ranges_starts_with_pred);64  }65 66  benchmark::Initialize(&argc, argv);67  benchmark::RunSpecifiedBenchmarks();68  benchmark::Shutdown();69  return 0;70}71