-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathalgorithm_bench.cpp
141 lines (115 loc) · 5.04 KB
/
algorithm_bench.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright (C) 2023 Adam Lugowski. All rights reserved.
// Use of this source code is governed by:
// the BSD 2-clause license, the MIT license, or at your choosing the BSL-1.0 license found in the LICENSE.*.txt files.
// SPDX-License-Identifier: BSD-2-Clause OR MIT OR BSL-1.0
#include <algorithm>
#include <vector>
#include <benchmark/benchmark.h>
#include "utils.hpp"
#include <poolstl/algorithm>
////////////////////////////////
template <class ExecPolicy>
void all_of(benchmark::State& state) {
auto values = iota_vector(arr_length);
for ([[maybe_unused]] auto _ : state) {
if constexpr (is_policy<ExecPolicy>::value) {
auto res = std::all_of(policy<ExecPolicy>::get(), values.begin(), values.end(), [&](auto v) { return v >= 0; });
benchmark::DoNotOptimize(res);
} else {
auto res = std::all_of(values.begin(), values.end(), [&](auto v) { return v >= 0; });
benchmark::DoNotOptimize(res);
}
benchmark::ClobberMemory();
}
}
BENCHMARK(all_of<seq>)->Name("all_of()")->UseRealTime();
BENCHMARK(all_of<poolstl_par>)->Name("all_of(poolstl::par)")->UseRealTime();
#ifdef POOLSTL_BENCH_STD_PAR
BENCHMARK(all_of<std_par>)->Name("all_of(std::execution::par)")->UseRealTime();
#endif
////////////////////////////////
template <class ExecPolicy>
void find_if(benchmark::State& state) {
auto values = iota_vector<int>(arr_length);
std::vector<int> haystack(arr_length);
double needle_frac = ((double)state.range(0)) / 100;
int needle_val = (int)((double)values.size() * needle_frac);
auto needle = [&needle_val](auto test) { return test >= needle_val; };
for ([[maybe_unused]] auto _ : state) {
if constexpr (is_policy<ExecPolicy>::value) {
auto res = std::find_if(policy<ExecPolicy>::get(), values.begin(), values.end(), needle);
benchmark::DoNotOptimize(res);
} else {
auto res = std::find_if(values.begin(), values.end(), needle);
benchmark::DoNotOptimize(res);
}
benchmark::ClobberMemory();
}
}
BENCHMARK(find_if<seq>)->Name("find_if()")->UseRealTime()->ArgName("needle_percentile")->Arg(5)->Arg(50)->Arg(100);
BENCHMARK(find_if<poolstl_par>)->Name("find_if(poolstl::par)")->UseRealTime()->ArgName("needle_percentile")->Arg(5)->Arg(50)->Arg(100);
#ifdef POOLSTL_BENCH_STD_PAR
BENCHMARK(find_if<std_par>)->Name("find_if(std::execution::par)")->UseRealTime()->ArgName("needle_percentile")->Arg(5)->Arg(50)->Arg(100);
#endif
////////////////////////////////
template <class ExecPolicy>
void for_each(benchmark::State& state) {
auto values = iota_vector<int>(arr_length);
std::vector<int> dest(arr_length);
for ([[maybe_unused]] auto _ : state) {
if constexpr (is_policy<ExecPolicy>::value) {
std::for_each(policy<ExecPolicy>::get(), values.begin(), values.end(), [&](auto v) { slow(); dest[v] = v; });
} else {
std::for_each(values.begin(), values.end(), [&](auto v) {slow(); dest[v] = v;});
}
benchmark::DoNotOptimize(dest);
benchmark::ClobberMemory();
}
}
BENCHMARK(for_each<seq>)->Name("for_each()")->UseRealTime();
BENCHMARK(for_each<poolstl_par>)->Name("for_each(poolstl::par)")->UseRealTime();
#ifdef POOLSTL_BENCH_STD_PAR
BENCHMARK(for_each<std_par>)->Name("for_each(std::execution::par)")->UseRealTime();
#endif
////////////////////////////////
template <class ExecPolicy>
void sort(benchmark::State& state) {
auto source = random_vector<int>(arr_length / 10);
for ([[maybe_unused]] auto _ : state) {
state.PauseTiming();
std::vector<int> values(source);
state.ResumeTiming();
if constexpr (is_policy<ExecPolicy>::value) {
std::sort(policy<ExecPolicy>::get(), values.begin(), values.end());
} else {
std::sort(values.begin(), values.end());
}
benchmark::DoNotOptimize(values);
benchmark::ClobberMemory();
}
}
BENCHMARK(sort<seq>)->Name("sort()")->UseRealTime();
BENCHMARK(sort<poolstl_par>)->Name("sort(poolstl::par)")->UseRealTime();
#ifdef POOLSTL_BENCH_STD_PAR
BENCHMARK(sort<std_par>)->Name("sort(std::execution::par)")->UseRealTime();
#endif
////////////////////////////////
template <class ExecPolicy>
void transform(benchmark::State& state) {
auto values = iota_vector<int>(arr_length);
std::vector<int> dest(arr_length);
for ([[maybe_unused]] auto _ : state) {
if constexpr (is_policy<ExecPolicy>::value) {
std::transform(policy<ExecPolicy>::get(), values.begin(), values.end(), dest.begin(), [&](auto v) { slow(); return v; });
} else {
std::transform(values.begin(), values.end(), dest.begin(), [&](auto v) { slow(); return v; });
}
benchmark::DoNotOptimize(dest);
benchmark::ClobberMemory();
}
}
BENCHMARK(transform<seq>)->Name("transform()")->UseRealTime();
BENCHMARK(transform<poolstl_par>)->Name("transform(poolstl::par)")->UseRealTime();
#ifdef POOLSTL_BENCH_STD_PAR
BENCHMARK(transform<std_par>)->Name("transform(std::execution::par)")->UseRealTime();
#endif