-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathsort_benchmark.cpp
93 lines (74 loc) · 3.02 KB
/
sort_benchmark.cpp
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
#include "nanobench.h"
#include <flux.hpp>
#include <algorithm>
#include <numeric>
#include <random>
namespace an = ankerl::nanobench;
static constexpr int test_sz = 100'000;
template <typename SortFn, typename Vec>
static void test_sort(const char* name, SortFn& sort, const Vec& vec, an::Bench& bench)
{
bench.run(name, [&] {
Vec copy = vec;
sort(copy);
bench.doNotOptimizeAway(copy);
});
}
int main()
{
{
std::vector<int> vec(test_sz);
std::mt19937 gen{std::random_device{}()};
std::uniform_int_distribution dist(0, test_sz);
std::generate(vec.begin(), vec.end(), [&] { return dist(gen); });
auto bench = an::Bench().relative(true).minEpochIterations(10);
test_sort("random ints (std)", std::ranges::sort, vec, bench);
test_sort("random ints (flux)", flux::sort, vec, bench);
}
{
std::vector<int> vec(test_sz);
std::iota(vec.begin(), vec.end(), 0);
auto bench = an::Bench().relative(true).minEpochIterations(10);
test_sort("sorted ints (std)", std::ranges::sort, vec, bench);
test_sort("sorted ints (flux)", flux::sort, vec, bench);
}
{
std::vector<int> vec(test_sz);
std::iota(vec.begin(), vec.end(), 0);
std::ranges::reverse(vec);
auto bench = an::Bench().relative(true).minEpochIterations(10);
test_sort("reverse sorted ints (std)", std::ranges::sort, vec, bench);
test_sort("reverse sorted ints (flux)", flux::sort, vec, bench);
}
{
std::vector<int> vec(test_sz);
std::iota(vec.begin(), vec.begin() + test_sz/2, 0);
std::iota(vec.begin() + test_sz/2, vec.end(), 0);
std::reverse(vec.begin() + test_sz/2, vec.end());
auto bench = an::Bench().relative(true).minEpochIterations(10);
test_sort("organpipe ints (std)", std::ranges::sort, vec, bench);
test_sort("organpipe ints (flux)", flux::sort, vec, bench);
}
{
std::vector<double> vec(test_sz);
std::mt19937 gen{std::random_device{}()};
std::uniform_real_distribution<double> dist(0, test_sz);
std::generate(vec.begin(), vec.end(), [&] { return dist(gen); });
auto bench = an::Bench().relative(true).minEpochIterations(10);
test_sort("random doubles (std)", std::ranges::sort, vec, bench);
// Use a custom comparator because we know we don't have NaNs
auto custom_sort = [](auto& arg) {
return flux::sort(arg, std::compare_weak_order_fallback);
};
test_sort("random doubles (flux)", custom_sort, vec, bench);
}
{
std::vector<std::string> vec(test_sz);
std::mt19937 gen{std::random_device{}()};
std::uniform_int_distribution dist(0, test_sz);
std::generate(vec.begin(), vec.end(), [&] { return std::to_string(dist(gen)); });
auto bench = an::Bench().relative(true);
test_sort("random strings (std)", std::ranges::sort, vec, bench);
test_sort("random strings (flux)", flux::sort, vec, bench);
}
}