Repository navigation
Expand file tree
/
Copy pathbench_min_max.cpp
More file actions
114 lines (99 loc) · 3.65 KB
/
Copy pathbench_min_max.cpp
File metadata and controls
114 lines (99 loc) · 3.65 KB
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
#include <algorithm>
#include <execution>
#include <vector>
#include <benchmark/benchmark.h>
#include "xsimd_algorithm/stl/max_element.hpp"
#include "xsimd_algorithm/stl/min_element.hpp"
#define BENCH_ARGS ->Arg(kSmall)->Arg(kMedium)->Arg(kLarge)
namespace
{
template <class ForwardIterator>
typename std::iterator_traits<ForwardIterator>::value_type dummy_std_max_value(ForwardIterator first, ForwardIterator last) noexcept
{
auto candidate = std::numeric_limits<typename std::iterator_traits<ForwardIterator>::value_type>::lowest();
for (; first != last; ++first)
{
if (candidate < *first)
{
candidate = *first;
}
}
return candidate;
}
template <typename T>
std::vector<T> make_input(std::size_t n, T constant)
{
std::vector<T> v(n);
std::fill(v.begin(), v.end(), constant);
return v;
}
template <typename T>
void BM_std_max_element(benchmark::State& state)
{
const auto constant = T { 5 };
auto src = make_input<T>(static_cast<std::size_t>(state.range(0)), constant);
src[state.range(0)-1] = T { 10 }; // Ensure there is a maximum value at the end
for (auto _ : state)
{
auto result = std::max_element(src.begin(), src.end());
benchmark::DoNotOptimize(result);
benchmark::ClobberMemory();
}
state.SetItemsProcessed(state.iterations() * state.range(0));
}
template <typename T>
void BM_xsimd_max_element(benchmark::State& state)
{
const auto constant = T { 5 };
auto src = make_input<T>(static_cast<std::size_t>(state.range(0)), constant);
src[state.range(0)-1] = T { 10 }; // Ensure there is a maximum value at the end
for (auto _ : state)
{
auto result = xsimd::max_element(src.begin(), src.end());
benchmark::DoNotOptimize(result);
benchmark::ClobberMemory();
}
state.SetItemsProcessed(state.iterations() * state.range(0));
}
template <typename T>
void BM_std_min_element(benchmark::State& state)
{
const auto constant = T { 5 };
auto src = make_input<T>(static_cast<std::size_t>(state.range(0)), constant);
src[state.range(0)-1] = T { 10 }; // Ensure there is a maximum value at the end
for (auto _ : state)
{
auto result = std::min_element(src.begin(), src.end());
benchmark::DoNotOptimize(result);
benchmark::ClobberMemory();
}
state.SetItemsProcessed(state.iterations() * state.range(0));
}
template <typename T>
void BM_xsimd_min_element(benchmark::State& state)
{
const auto constant = T { 5 };
auto src = make_input<T>(static_cast<std::size_t>(state.range(0)), constant);
src[state.range(0)-1] = T { 10 }; // Ensure there is a maximum value at the end
for (auto _ : state)
{
auto result = xsimd::min_element(src.begin(), src.end());
benchmark::DoNotOptimize(result);
benchmark::ClobberMemory();
}
state.SetItemsProcessed(state.iterations() * state.range(0));
}
static constexpr long long kSmall = 1LL << 12;
static constexpr long long kMedium = 1LL << 18;
static constexpr long long kLarge = 1LL << 22;
// max_element
BENCHMARK_TEMPLATE(BM_std_max_element, float)
BENCH_ARGS;
BENCHMARK_TEMPLATE(BM_xsimd_max_element, float)
BENCH_ARGS;
// min_element
BENCHMARK_TEMPLATE(BM_std_min_element, float)
BENCH_ARGS;
BENCHMARK_TEMPLATE(BM_xsimd_min_element, float)
BENCH_ARGS;
}