1+ #include < algorithm>
2+ #include < vector>
3+
4+ #include < benchmark/benchmark.h>
5+
6+ #include " xsimd_algorithm/stl/count.hpp"
7+
8+ #define BENCH_ARGS ->Arg (kSmall )->Arg(kMedium )->Arg(kLarge )
9+
10+ namespace
11+ {
12+ template <typename T>
13+ std::vector<T> make_input (std::size_t n, T constant)
14+ {
15+ std::vector<T> v (n);
16+ std::fill (v.begin (), v.end (), constant);
17+ return v;
18+ }
19+
20+ template <typename T>
21+ void BM_std_count (benchmark::State& state)
22+ {
23+ const auto constant = T { 5 };
24+
25+ auto src = make_input<T>(static_cast <std::size_t >(state.range (0 )), constant);
26+
27+ for (auto _ : state)
28+ {
29+ auto result = std::count (src.begin (), src.end (), constant);
30+ benchmark::DoNotOptimize (result);
31+ benchmark::ClobberMemory ();
32+ }
33+ state.SetItemsProcessed (state.iterations () * state.range (0 ));
34+ }
35+
36+ template <typename T>
37+ void BM_xsimd_count (benchmark::State& state)
38+ {
39+ const auto constant = T { 5 };
40+ auto src = make_input<T>(static_cast <std::size_t >(state.range (0 )), constant);
41+ for (auto _ : state)
42+ {
43+ auto result = xsimd::count (src.begin (), src.end (), constant);
44+ benchmark::DoNotOptimize (result);
45+ benchmark::ClobberMemory ();
46+ }
47+ state.SetItemsProcessed (state.iterations () * state.range (0 ));
48+ }
49+
50+
51+ static constexpr long long kSmall = 1LL << 12 ;
52+ static constexpr long long kMedium = 1LL << 18 ;
53+ static constexpr long long kLarge = 1LL << 22 ;
54+
55+ // count
56+ BENCHMARK_TEMPLATE (BM_std_count, float )
57+ BENCH_ARGS ;
58+ BENCHMARK_TEMPLATE (BM_xsimd_count, float )
59+ BENCH_ARGS ;
60+
61+ }
0 commit comments