llvm-project/libcxx/test/benchmarks/bitset.bench.cpp
Nikolas Klauser fbde9240e9
[libc++] Remove some low value benchmarks (#175178)
This patch removes some of the benchmarks in our suite that are
relatively low value. Specifically:
- `lexicographical_compare` isn't benchmarked with every number of
    elements between 1 and 8 anymore
- `bitset` isn't benchmarked with as many sizes anymore. Instead, only
    some representative sizes are benchmarked.
- `to_chars` isn't benchmarked for every possible base anymore, but only
    2, 8, 10, 16 and 23 (so we have an uncommon base as well)
2026-01-12 14:09:54 -05:00

82 lines
2.8 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
#include "benchmark/benchmark.h"
#include <bitset>
#include <cmath>
#include <cstddef>
#include <random>
template <std::size_t N>
struct GenerateBitset {
// Construct a bitset with N bits, where each bit is set with probability p.
static std::bitset<N> generate(double p) {
std::bitset<N> b;
if (p <= 0.0)
return b;
if (p >= 1.0)
return ~b;
std::random_device rd;
std::mt19937 gen(rd());
std::bernoulli_distribution d(p);
for (std::size_t i = 0; i < N; ++i)
b[i] = d(gen);
return b;
}
static std::bitset<N> sparse() { return generate(0.1); }
static std::bitset<N> dense() { return generate(0.9); }
static std::bitset<N> uniform() { return generate(0.5); }
};
template <std::size_t N>
static void BM_BitsetToString(benchmark::State& state) {
double p = state.range(0) / 100.0;
std::bitset<N> b = GenerateBitset<N>::generate(p);
benchmark::DoNotOptimize(b);
for (auto _ : state) {
benchmark::DoNotOptimize(b.to_string());
}
}
// Sparse bitset
BENCHMARK(BM_BitsetToString<32>)->Arg(10)->Name("BM_BitsetToString<32>/Sparse (10%)");
BENCHMARK(BM_BitsetToString<64>)->Arg(10)->Name("BM_BitsetToString<64>/Sparse (10%)");
BENCHMARK(BM_BitsetToString<8192>)->Arg(10)->Name("BM_BitsetToString<8192>/Sparse (10%)");
BENCHMARK(BM_BitsetToString<1048576>)->Arg(10)->Name("BM_BitsetToString<1048576>/Sparse (10%)"); // 1 << 20
// Dense bitset
BENCHMARK(BM_BitsetToString<32>)->Arg(90)->Name("BM_BitsetToString<32>/Dense (90%)");
BENCHMARK(BM_BitsetToString<64>)->Arg(90)->Name("BM_BitsetToString<64>/Dense (90%)");
BENCHMARK(BM_BitsetToString<8192>)->Arg(90)->Name("BM_BitsetToString<8192>/Dense (90%)");
BENCHMARK(BM_BitsetToString<1048576>)->Arg(90)->Name("BM_BitsetToString<1048576>/Dense (90%)"); // 1 << 20
// Uniform bitset
BENCHMARK(BM_BitsetToString<32>)->Arg(50)->Name("BM_BitsetToString<32>/Uniform (50%)");
BENCHMARK(BM_BitsetToString<64>)->Arg(50)->Name("BM_BitsetToString<64>/Uniform (50%)");
BENCHMARK(BM_BitsetToString<8192>)->Arg(50)->Name("BM_BitsetToString<8192>/Uniform (50%)");
BENCHMARK(BM_BitsetToString<1048576>)->Arg(50)->Name("BM_BitsetToString<1048576>/Uniform (50%)"); // 1 << 20
static void BM_Bitset_ctor_ull(benchmark::State& state) {
unsigned long long val = 1;
for (auto _ : state) {
benchmark::DoNotOptimize(val);
std::bitset<128> b(val);
benchmark::DoNotOptimize(b);
}
}
BENCHMARK(BM_Bitset_ctor_ull);
BENCHMARK_MAIN();