blob: ab0e81b0cac9b322d5ffbb15185ebc3a9b87c214 [file] [log] [blame]
Eric Fiselier9e3e1372016-07-19 23:07:03 +00001#include <unordered_set>
2#include <vector>
3#include <cstdint>
4
Eric Fiselier87038cd2018-01-18 04:23:01 +00005#include "benchmark/benchmark.h"
Eric Fiselier9e3e1372016-07-19 23:07:03 +00006#include "GenerateInput.hpp"
7
8constexpr std::size_t TestNumInputs = 1024;
9
10template <class GenInputs>
11void BM_Sort(benchmark::State& st, GenInputs gen) {
12 using ValueType = typename decltype(gen(0))::value_type;
Eric Fiseliere5e33e42016-08-09 18:56:48 +000013 const auto in = gen(st.range(0));
Eric Fiselier9e3e1372016-07-19 23:07:03 +000014 std::vector<ValueType> inputs[5];
15 auto reset_inputs = [&]() {
16 for (auto& C : inputs) {
17 C = in;
18 benchmark::DoNotOptimize(C.data());
19 }
20 };
21 reset_inputs();
22 while (st.KeepRunning()) {
23 for (auto& I : inputs) {
24 std::sort(I.data(), I.data() + I.size());
25 benchmark::DoNotOptimize(I.data());
26 }
27 st.PauseTiming();
28 reset_inputs();
29 benchmark::ClobberMemory();
30 st.ResumeTiming();
31 }
32}
33
34BENCHMARK_CAPTURE(BM_Sort, random_uint32,
35 getRandomIntegerInputs<uint32_t>)->Arg(TestNumInputs);
36
37BENCHMARK_CAPTURE(BM_Sort, sorted_ascending_uint32,
38 getSortedIntegerInputs<uint32_t>)->Arg(TestNumInputs);
39
40BENCHMARK_CAPTURE(BM_Sort, sorted_descending_uint32,
41 getReverseSortedIntegerInputs<uint32_t>)->Arg(TestNumInputs);
42
43BENCHMARK_CAPTURE(BM_Sort, single_element_uint32,
44 getDuplicateIntegerInputs<uint32_t>)->Arg(TestNumInputs);
45
46BENCHMARK_CAPTURE(BM_Sort, pipe_organ_uint32,
47 getPipeOrganIntegerInputs<uint32_t>)->Arg(TestNumInputs);
48
49BENCHMARK_CAPTURE(BM_Sort, random_strings,
50 getRandomStringInputs)->Arg(TestNumInputs);
51
52BENCHMARK_CAPTURE(BM_Sort, sorted_ascending_strings,
53 getSortedStringInputs)->Arg(TestNumInputs);
54
55BENCHMARK_CAPTURE(BM_Sort, sorted_descending_strings,
56 getReverseSortedStringInputs)->Arg(TestNumInputs);
57
58BENCHMARK_CAPTURE(BM_Sort, single_element_strings,
59 getDuplicateStringInputs)->Arg(TestNumInputs);
60
Eric Fiselier9ed59852018-10-29 19:25:02 +000061template <typename GenInputs, typename Alg>
62void do_binary_search_benchmark(benchmark::State& st, GenInputs gen, Alg alg)
63{
64 using ValueType = typename decltype(gen(0))::value_type;
65 auto in = gen(st.range(0));
66 std::sort(in.begin(), in.end());
67
68 const auto every_10_percentile = [&]() -> std::vector<ValueType*> {
69 size_t step = in.size() / 10;
70
71 if (step == 0) {
72 st.SkipWithError("Input doesn't contain enough elements");
73 return {};
74 }
75
76 std::vector<ValueType*> res;
77 for (size_t i = 0; i < in.size(); i += step)
78 res.push_back(&in[i]);
79
80 return res;
81 }();
82
83 for (auto _ : st)
84 {
85 for (auto* test : every_10_percentile)
86 benchmark::DoNotOptimize(alg(in.begin(), in.end(), *test));
87 }
88}
89
90template <typename GenInputs>
91void BM_LowerBound(benchmark::State& st, GenInputs gen)
92{
93 do_binary_search_benchmark(st, gen, [](auto f, auto l, const auto& v) {
94 return std::lower_bound(f, l, v);
95 });
96}
97
98BENCHMARK_CAPTURE(BM_LowerBound, random_int32, getRandomIntegerInputs<int32_t>)
99 ->Arg(TestNumInputs) // Small int32_t vector
100 ->Arg(TestNumInputs * TestNumInputs); // Big int32_t vector
101
102BENCHMARK_CAPTURE(BM_LowerBound, random_int64, getRandomIntegerInputs<int64_t>)
103 ->Arg(TestNumInputs); // Small int64_t vector. Should also represent pointers.
104
105BENCHMARK_CAPTURE(BM_LowerBound, random_strings, getRandomStringInputs)
106 ->Arg(TestNumInputs); // Small string vector. What happens if the comparison is not very cheap.
107
108template <typename GenInputs>
109void BM_EqualRange(benchmark::State& st, GenInputs gen)
110{
111 do_binary_search_benchmark(st, gen, [](auto f, auto l, const auto& v) {
112 return std::equal_range(f, l, v);
113 });
114}
115
116BENCHMARK_CAPTURE(BM_EqualRange, random_int32, getRandomIntegerInputs<int32_t>)
117 ->Arg(TestNumInputs) // Small int32_t vector
118 ->Arg(TestNumInputs * TestNumInputs); // Big int32_t vector
119
120BENCHMARK_CAPTURE(BM_EqualRange, random_int64, getRandomIntegerInputs<int64_t>)
121 ->Arg(TestNumInputs); // Small int64_t vector. Should also represent pointers.
122
123BENCHMARK_CAPTURE(BM_EqualRange, random_strings, getRandomStringInputs)
124 ->Arg(TestNumInputs); // Small string vector. What happens if the comparison is not very cheap.
Eric Fiselier9e3e1372016-07-19 23:07:03 +0000125
Eric Fiselier87038cd2018-01-18 04:23:01 +0000126BENCHMARK_MAIN();