Nikolas Klauser a2fb416b53
[libc++] Rewrite the std::lower_bound benchmark to be more efficient and add an upper_bound benchmark (#177180)
The current benchmark is incredibly slow to run. This patch refactors
the benchmark to be faster and also adds an equivalent benchmark for
`std::upper_bound`.

Fixes #177026
2026-01-27 13:46:36 -05:00

87 lines
3.2 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 <deque>
#include <forward_list>
#include <list>
#include <numeric>
#include <random>
#include <vector>
#include "benchmark/benchmark.h"
#include "../../GenerateInput.h"
int main(int argc, char** argv) {
{
auto bm = []<class Container>(benchmark::State& state) static {
std::mt19937_64 rng{123456};
using value_type = typename Container::value_type;
std::vector<value_type> vec;
for (int64_t i = 0; i != state.range(); ++i)
vec.emplace_back(Generate<typename Container::value_type>::random());
std::sort(vec.begin(), vec.end());
Container c(vec.begin(), vec.end());
for (auto _ : state) {
auto result = std::lower_bound(c.begin(), c.end(), vec[rng() % vec.size()]);
benchmark::DoNotOptimize(result);
benchmark::DoNotOptimize(vec);
}
};
auto register_benchmark = [&]<class Container>(std::type_identity<Container>, std::string name) {
benchmark::RegisterBenchmark(name, bm.template operator()<Container>)->Arg(8)->Arg(100)->Arg(8192);
};
register_benchmark(std::type_identity<std::vector<int>>{}, "std::lower_bound(std::vector<int>)");
register_benchmark(std::type_identity<std::deque<int>>{}, "std::lower_bound(std::deque<int>)");
register_benchmark(std::type_identity<std::list<int>>{}, "std::lower_bound(std::list<int>)");
register_benchmark(std::type_identity<std::forward_list<int>>{}, "std::lower_bound(std::forward_list<int>)");
}
{
auto bm = []<class Container>(benchmark::State& state) static {
std::mt19937_64 rng{123456};
using value_type = typename Container::value_type;
std::vector<value_type> vec;
for (int64_t i = 0; i != state.range(); ++i)
vec.emplace_back(Generate<typename Container::value_type>::random());
std::sort(vec.begin(), vec.end());
Container c(vec.begin(), vec.end());
for (auto _ : state) {
auto result = std::upper_bound(c.begin(), c.end(), vec[rng() % vec.size()]);
benchmark::DoNotOptimize(result);
benchmark::DoNotOptimize(vec);
}
};
auto register_benchmark = [&]<class Container>(std::type_identity<Container>, std::string name) {
benchmark::RegisterBenchmark(name, bm.template operator()<Container>)->Arg(8)->Arg(100)->Arg(8192);
};
register_benchmark(std::type_identity<std::vector<int>>{}, "std::upper_bound(std::vector<int>)");
register_benchmark(std::type_identity<std::deque<int>>{}, "std::upper_bound(std::deque<int>)");
register_benchmark(std::type_identity<std::list<int>>{}, "std::upper_bound(std::list<int>)");
register_benchmark(std::type_identity<std::forward_list<int>>{}, "std::upper_bound(std::forward_list<int>)");
}
benchmark::Initialize(&argc, argv);
benchmark::RunSpecifiedBenchmarks();
benchmark::Shutdown();
return 0;
}