Skip to content

Commit af4f9c8

Browse files
Benchmark count for vector<bool> (#5684)
Co-authored-by: Stephan T. Lavavej <stl@microsoft.com>
1 parent 023485c commit af4f9c8

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

benchmarks/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,5 @@ add_benchmark(swap_ranges src/swap_ranges.cpp)
135135
add_benchmark(unique src/unique.cpp)
136136
add_benchmark(vector_bool_copy src/vector_bool_copy.cpp)
137137
add_benchmark(vector_bool_copy_n src/vector_bool_copy_n.cpp)
138+
add_benchmark(vector_bool_count src/vector_bool_count.cpp)
138139
add_benchmark(vector_bool_move src/vector_bool_move.cpp)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3+
4+
#include <benchmark/benchmark.h>
5+
//
6+
#include <algorithm>
7+
#include <cstddef>
8+
#include <random>
9+
#include <vector>
10+
11+
using namespace std;
12+
13+
vector<bool> createRandomVector(const size_t size) {
14+
mt19937 gen;
15+
bernoulli_distribution dist{0.5};
16+
vector<bool> result(size);
17+
generate(result.begin(), result.end(), [&] { return dist(gen); });
18+
return result;
19+
}
20+
21+
void count_aligned(benchmark::State& state) {
22+
const auto size = static_cast<size_t>(state.range(0));
23+
vector<bool> v = createRandomVector(size);
24+
25+
bool b = false;
26+
27+
for (auto _ : state) {
28+
benchmark::DoNotOptimize(b);
29+
benchmark::DoNotOptimize(v);
30+
auto r = count(v.cbegin(), v.cend(), b);
31+
benchmark::DoNotOptimize(r);
32+
b = !b;
33+
}
34+
}
35+
36+
37+
BENCHMARK(count_aligned)->RangeMultiplier(64)->Range(64, 64 << 10);
38+
39+
BENCHMARK_MAIN();

0 commit comments

Comments
 (0)