60 lines
1.9 KiB
C++

//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
#include <algorithm>
#include <cstddef>
#include <deque>
#include <iterator>
#include <list>
#include <vector>
#include <benchmark/benchmark.h>
#include "../../GenerateInput.h"
int main(int argc, char** argv) {
// Benchmark ranges::contains where we process the whole sequence, which is the
// worst case.
{
auto bm = []<class Container>(std::string name) {
benchmark::RegisterBenchmark(
name,
[](auto& st) {
std::size_t const size = st.range(0);
using ValueType = typename Container::value_type;
ValueType x = Generate<ValueType>::random();
ValueType y = random_different_from({x});
Container c(size, x);
auto first = c.begin();
auto last = c.end();
for ([[maybe_unused]] auto _ : st) {
benchmark::DoNotOptimize(c);
benchmark::DoNotOptimize(y);
auto result = std::ranges::contains(first, last, y);
benchmark::DoNotOptimize(result);
}
})
->Arg(8)
->Arg(32)
->Arg(50) // non power-of-two
->Arg(8192)
->Arg(1 << 20);
};
bm.operator()<std::vector<int>>("rng::contains(vector<int>) (process all)");
bm.operator()<std::deque<int>>("rng::contains(deque<int>) (process all)");
bm.operator()<std::list<int>>("rng::contains(list<int>) (process all)");
}
benchmark::Initialize(&argc, argv);
benchmark::RunSpecifiedBenchmarks();
benchmark::Shutdown();
return 0;
}