|
| 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 <functional> |
| 9 | +#include <vector> |
| 10 | + |
| 11 | +#include "skewed_allocator.hpp" |
| 12 | + |
| 13 | +using namespace std; |
| 14 | + |
| 15 | +enum class alg { any_, all_, none_ }; |
| 16 | +enum class content { ones_then_zeros, zeros_then_ones }; |
| 17 | + |
| 18 | +template <alg Alg, content Content, class Pred = identity> |
| 19 | +void meow_of(benchmark::State& state) { |
| 20 | + const auto size = static_cast<size_t>(state.range(0)); |
| 21 | + vector<bool, not_highly_aligned_allocator<bool>> source(size); |
| 22 | + |
| 23 | + if constexpr (Content == content::ones_then_zeros) { |
| 24 | + fill(source.begin(), source.begin() + source.size() / 2, true); |
| 25 | + } else { |
| 26 | + fill(source.begin() + source.size() / 2, source.end(), true); |
| 27 | + } |
| 28 | + |
| 29 | + for (auto _ : state) { |
| 30 | + benchmark::DoNotOptimize(source); |
| 31 | + bool result; |
| 32 | + if constexpr (Alg == alg::any_) { |
| 33 | + result = any_of(source.begin(), source.end(), Pred{}); |
| 34 | + } else if constexpr (Alg == alg::all_) { |
| 35 | + result = all_of(source.begin(), source.end(), Pred{}); |
| 36 | + } else { |
| 37 | + result = none_of(source.begin(), source.end(), Pred{}); |
| 38 | + } |
| 39 | + benchmark::DoNotOptimize(result); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +void common_args(auto bm) { |
| 44 | + bm->RangeMultiplier(64)->Range(64, 64 << 10); |
| 45 | +} |
| 46 | + |
| 47 | +using not_ = logical_not<>; |
| 48 | + |
| 49 | +BENCHMARK(meow_of<alg::any_, content::zeros_then_ones>)->Apply(common_args); |
| 50 | +BENCHMARK(meow_of<alg::any_, content::ones_then_zeros, not_>)->Apply(common_args); |
| 51 | +BENCHMARK(meow_of<alg::all_, content::ones_then_zeros>)->Apply(common_args); |
| 52 | +BENCHMARK(meow_of<alg::none_, content::zeros_then_ones>)->Apply(common_args); |
| 53 | + |
| 54 | +BENCHMARK_MAIN(); |
0 commit comments