-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbenchmark.cpp
76 lines (69 loc) · 2.18 KB
/
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
#include <vector>
#include <iostream>
#include <algorithm>
#include <chrono>
#include "randutils.hpp" // see https://gist.github.com/imneme/540829265469e673d045
#include <boost/sort/spreadsort/spreadsort.hpp>
#include "drop_merge_sort.hpp"
using namespace std::chrono;
randutils::default_rng rng;
template<typename T>
std::vector<T> randomize(float factor) {
int max = 1000000;
std::vector<T> ret(max);
for (int i = 0; i < max; ++i) {
if (rng.uniform(0.0, 1.0) < factor)
ret[i] = rng.uniform(0, max-1);
else
ret[i] = i;
}
return ret;
}
template<>
std::vector<std::string> randomize<std::string>(float factor) {
int max = 100000;
std::vector<std::string> ret(max);
for (int i = 0; i < max; ++i) {
std::string s;
if (rng.uniform(0.0, 1.0) < factor)
s = std::to_string(rng.uniform(0, max-1));
else
s = std::to_string(i);
ret[i] = std::string(100 - s.size(), '0') + s;
}
return ret;
}
template<typename T, typename Sort>
float measure(float factor, Sort sort) {
size_t total = 0;
constexpr size_t sz = 5;
for (int i = 0; i < sz; ++i) {
auto tab = randomize<T>(factor);
//auto copy = tab;
auto t1 = high_resolution_clock::now();
sort(tab);
auto t2 = high_resolution_clock::now();
size_t time = duration_cast<microseconds>(t2-t1).count();
total += time;
// sanity check
//std::sort(copy.begin(), copy.end());
//if(copy != tab)
// throw "asdf";
}
return total / (1000.0 * sz);
}
template<typename T>
void benchmark() {
for (int i = 0; i <= 100; ++i) {
float factor = i * 0.01;
auto dt1 = measure<T>(factor, [](auto &tab){dmsort(std::begin(tab), std::end(tab));});
auto dt2 = measure<T>(factor, [](auto &tab){std::sort(std::begin(tab), std::end(tab));});
auto dt3 = measure<T>(factor, [](auto &tab){boost::sort::spreadsort::spreadsort(std::begin(tab), std::end(tab));});
std::cout << (factor) << "\t" << dt1 << "\t" << dt2 << "\t" << dt3 << "\n";
}
std::cout << "\n";
}
int main(){
benchmark<int>();
benchmark<std::string>();
}