1+ #include < algorithm>
2+ #include < vector>
3+
4+ #include < benchmark/benchmark.h>
5+
6+ #include " xsimd_algorithm/stl/all_of.hpp"
7+ #include " xsimd_algorithm/stl/any_of.hpp"
8+ #include " xsimd_algorithm/stl/none_of.hpp"
9+
10+ #define BENCH_ARGS ->Arg (kSmall )->Arg(kMedium )->Arg(kLarge )
11+
12+ namespace
13+ {
14+ template <typename T>
15+ std::vector<T> make_input (std::size_t n, T constant)
16+ {
17+ std::vector<T> v (n);
18+ std::fill (v.begin (), v.end (), constant);
19+ return v;
20+ }
21+
22+ template <typename T>
23+ void BM_std_all_of (benchmark::State& state)
24+ {
25+ const auto constant = T { 5 };
26+
27+ auto src = make_input<T>(static_cast <std::size_t >(state.range (0 )), constant);
28+ for (auto _ : state)
29+ {
30+ auto result = std::all_of (src.begin (), src.end (), [constant](const auto & x) { return x == constant; });
31+ benchmark::DoNotOptimize (result);
32+ benchmark::ClobberMemory ();
33+ }
34+ state.SetItemsProcessed (state.iterations () * state.range (0 ));
35+ }
36+
37+ template <typename T>
38+ void BM_xsimd_all_of (benchmark::State& state)
39+ {
40+ const auto constant = T { 5 };
41+ auto src = make_input<T>(static_cast <std::size_t >(state.range (0 )), constant);
42+ for (auto _ : state)
43+ {
44+ auto result = xsimd::all_of (src.begin (), src.end (), [constant](const auto & x) { return x == constant; });
45+ benchmark::DoNotOptimize (result);
46+ benchmark::ClobberMemory ();
47+ }
48+ state.SetItemsProcessed (state.iterations () * state.range (0 ));
49+ }
50+
51+ template <typename T>
52+ void BM_std_any_of (benchmark::State& state)
53+ {
54+ const auto constant = T { 5 };
55+ const auto tested = T { 10 };
56+ auto src = make_input<T>(static_cast <std::size_t >(state.range (0 )), constant);
57+ for (auto _ : state)
58+ {
59+ auto result = std::any_of (src.begin (), src.end (), [tested](const auto & x) { return x == tested; });
60+ benchmark::DoNotOptimize (result);
61+ benchmark::ClobberMemory ();
62+ }
63+ state.SetItemsProcessed (state.iterations () * state.range (0 ));
64+ }
65+
66+ template <typename T>
67+ void BM_xsimd_any_of (benchmark::State& state)
68+ {
69+ const auto constant = T { 5 };
70+ const auto tested = T { 10 };
71+ auto src = make_input<T>(static_cast <std::size_t >(state.range (0 )), constant);
72+ for (auto _ : state)
73+ {
74+ auto result = xsimd::any_of (src.begin (), src.end (), [tested](const auto & x) { return x == tested; });
75+ benchmark::DoNotOptimize (result);
76+ benchmark::ClobberMemory ();
77+ }
78+ state.SetItemsProcessed (state.iterations () * state.range (0 ));
79+ }
80+
81+ static constexpr long long kSmall = 1LL << 12 ;
82+ static constexpr long long kMedium = 1LL << 18 ;
83+ static constexpr long long kLarge = 1LL << 22 ;
84+
85+ // all_of
86+ BENCHMARK_TEMPLATE (BM_std_all_of, float )
87+ BENCH_ARGS ;
88+ BENCHMARK_TEMPLATE (BM_xsimd_all_of, float )
89+ BENCH_ARGS ;
90+ // any_of
91+ BENCHMARK_TEMPLATE (BM_std_any_of, float )
92+ BENCH_ARGS ;
93+ BENCHMARK_TEMPLATE (BM_xsimd_any_of, float )
94+ BENCH_ARGS ;
95+
96+ }
0 commit comments