Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions benchmarks/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -134,5 +134,6 @@ add_benchmark(sv_equal src/sv_equal.cpp)
add_benchmark(swap_ranges src/swap_ranges.cpp)
add_benchmark(unique src/unique.cpp)
add_benchmark(vector_bool_copy src/vector_bool_copy.cpp)
add_benchmark(vector_bool_count src/vector_bool_count.cpp)
add_benchmark(vector_bool_copy_n src/vector_bool_copy_n.cpp)
add_benchmark(vector_bool_move src/vector_bool_move.cpp)
38 changes: 38 additions & 0 deletions benchmarks/src/vector_bool_count.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) Microsoft Corporation.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#include <benchmark/benchmark.h>
//
#include <algorithm>
#include <cstddef>
#include <random>
#include <vector>

using namespace std;

vector<bool> createRandomVector(const size_t size) {
static mt19937 gen;
vector<bool> result(size);
generate_n(result.begin(), size, [] { return bernoulli_distribution{0.5}(gen); });
return result;
}

void count_aligned(benchmark::State& state) {
const auto size = static_cast<size_t>(state.range(0));
vector<bool> v = createRandomVector(size);

bool b = false;

for (auto _ : state) {
benchmark::DoNotOptimize(b);
benchmark::DoNotOptimize(v);
auto r = count(v.cbegin(), v.cend(), b);
benchmark::DoNotOptimize(r);
b = !b;
}
}


BENCHMARK(count_aligned)->RangeMultiplier(64)->Range(64, 64 << 10);

BENCHMARK_MAIN();