Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix <bitset> benchmark correctness #4817

Merged
merged 5 commits into from
Jul 11, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions benchmarks/src/bitset_to_string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,41 +13,52 @@
using namespace std;

namespace {
const auto random_bits = [] {
template <size_t N>
const auto random_bits_init() {
StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved
StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved
mt19937_64 rnd{};
array<uint64_t, 32> arr;
array<uint64_t, N> arr;
for (auto& d : arr) {
d = rnd();
}
return arr;
}();
}

template <size_t N = 32>
const auto random_bits = random_bits_init<N>();

template <size_t N, class charT>
void BM_bitset_to_string(benchmark::State& state) {
static_assert(N <= 64);

for (auto _ : state) {
for (const auto& bits : random_bits) {
for (const auto& bits : random_bits<>) {
benchmark::DoNotOptimize(bits);
bitset<N> bs{bits};
benchmark::DoNotOptimize(bs.to_string<charT>());
}
}
}

template <class charT>
template <size_t N, class charT>
void BM_bitset_to_string_large_single(benchmark::State& state) {
const auto large_bitset = bit_cast<bitset<CHAR_BIT * sizeof(random_bits)>>(random_bits);
static_assert(N % 64 == 0 && N >= 64);
const auto& bitset_data = random_bits<N / 64>;

const auto large_bitset = bit_cast<bitset<CHAR_BIT * sizeof(bitset_data)>>(bitset_data);
StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved
for (auto _ : state) {
benchmark::DoNotOptimize(large_bitset);
benchmark::DoNotOptimize(large_bitset.to_string<charT>());
}
}
} // namespace

BENCHMARK(BM_bitset_to_string<15, char>);
BENCHMARK(BM_bitset_to_string<64, char>);
BENCHMARK(BM_bitset_to_string<512, char>);
BENCHMARK(BM_bitset_to_string_large_single<char>);
BENCHMARK(BM_bitset_to_string_large_single<512, char>);
BENCHMARK(BM_bitset_to_string_large_single<2048, char>);
BENCHMARK(BM_bitset_to_string<7, wchar_t>);
BENCHMARK(BM_bitset_to_string<64, wchar_t>);
BENCHMARK(BM_bitset_to_string<512, wchar_t>);
BENCHMARK(BM_bitset_to_string_large_single<wchar_t>);
BENCHMARK(BM_bitset_to_string_large_single<512, wchar_t>);
BENCHMARK(BM_bitset_to_string_large_single<2048, wchar_t>);

BENCHMARK_MAIN();