1 // (C) Copyright Nick Thompson and Matt Borland 2020.
2 // Use, modification and distribution are subject to the
3 // Boost Software License, Version 1.0. (See accompanying file
4 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 #include <boost/math/statistics/univariate_statistics.hpp>
8 #include <benchmark/benchmark.h>
11 void test_mode(benchmark::State
& state
)
13 using boost::math::statistics::sorted_mode
;
15 std::random_device rd
;
16 std::mt19937_64
mt(rd());
17 std::uniform_int_distribution
<> dist
{1, 10};
19 auto gen
= [&dist
, &mt
](){return dist(mt
);};
21 std::vector
<Z
> v(state
.range(0));
22 std::generate(v
.begin(), v
.end(), gen
);
27 benchmark::DoNotOptimize(sorted_mode(v
.begin(), v
.end(), std::back_inserter(modes
)));
30 state
.SetComplexityN(state
.range(0));
34 void sequential_test_mode(benchmark::State
& state
)
36 using boost::math::statistics::sorted_mode
;
38 std::vector
<Z
> v(state
.range(0));
40 size_t current_num
{1};
41 // produces {1, 2, 3, 4, 5...}
42 for(size_t i
{}; i
< v
.size(); ++i
)
51 benchmark::DoNotOptimize(sorted_mode(v
, std::back_inserter(modes
)));
54 state
.SetComplexityN(state
.range(0));
58 void sequential_pairs_test_mode(benchmark::State
& state
)
60 using boost::math::statistics::sorted_mode
;
62 std::vector
<Z
> v(state
.range(0));
64 size_t current_num
{1};
65 size_t current_num_counter
{};
66 // produces {1, 1, 2, 2, 3, 3, ...}
67 for(size_t i
{}; i
< v
.size(); ++i
)
70 ++current_num_counter
;
71 if(current_num_counter
> 2)
74 current_num_counter
= 0;
81 benchmark::DoNotOptimize(sorted_mode(v
, std::back_inserter(modes
)));
84 state
.SetComplexityN(state
.range(0));
88 void sequential_multiple_test_mode(benchmark::State
& state
)
90 using boost::math::statistics::sorted_mode
;
92 std::vector
<Z
> v(state
.range(0));
94 size_t current_num
{1};
95 size_t current_num_counter
{};
96 // produces {1, 2, 2, 3, 3, 3, 4, 4, 4, 4, ...}
97 for(size_t i
{}; i
< v
.size(); ++i
)
100 ++current_num_counter
;
101 if(current_num_counter
> current_num
)
104 current_num_counter
= 0;
110 std::vector
<Z
> modes
;
111 benchmark::DoNotOptimize(sorted_mode(v
, std::back_inserter(modes
)));
114 state
.SetComplexityN(state
.range(0));
117 BENCHMARK_TEMPLATE(test_mode
, int32_t)->RangeMultiplier(2)->Range(1<<1, 1<<22)->Complexity();
118 BENCHMARK_TEMPLATE(test_mode
, int64_t)->RangeMultiplier(2)->Range(1<<1, 1<<22)->Complexity();
119 BENCHMARK_TEMPLATE(test_mode
, uint32_t)->RangeMultiplier(2)->Range(1<<1, 1<<22)->Complexity();
120 BENCHMARK_TEMPLATE(sequential_test_mode
, int32_t)->RangeMultiplier(2)->Range(1<<1, 1<<22)->Complexity();
121 BENCHMARK_TEMPLATE(sequential_test_mode
, int64_t)->RangeMultiplier(2)->Range(1<<1, 1<<22)->Complexity();
122 BENCHMARK_TEMPLATE(sequential_test_mode
, uint32_t)->RangeMultiplier(2)->Range(1<<1, 1<<22)->Complexity();
123 BENCHMARK_TEMPLATE(sequential_pairs_test_mode
, int32_t)->RangeMultiplier(2)->Range(1<<1, 1<<22)->Complexity();
124 BENCHMARK_TEMPLATE(sequential_pairs_test_mode
, int64_t)->RangeMultiplier(2)->Range(1<<1, 1<<22)->Complexity();
125 BENCHMARK_TEMPLATE(sequential_pairs_test_mode
, uint32_t)->RangeMultiplier(2)->Range(1<<1, 1<<22)->Complexity();
126 BENCHMARK_TEMPLATE(sequential_multiple_test_mode
, int32_t)->RangeMultiplier(2)->Range(1<<1, 1<<22)->Complexity();
127 BENCHMARK_TEMPLATE(sequential_multiple_test_mode
, int64_t)->RangeMultiplier(2)->Range(1<<1, 1<<22)->Complexity();
128 BENCHMARK_TEMPLATE(sequential_multiple_test_mode
, uint32_t)->RangeMultiplier(2)->Range(1<<1, 1<<22)->Complexity();