This repository has been archived by the owner on Nov 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark.cpp
67 lines (57 loc) · 2.45 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
#include <iostream>
#include <random>
#include <thread>
#include <cassert>
#include <algorithm>
#include <chrono>
#include <fstream>
#include "BucketSort.h"
constexpr auto numreps = 10U;
constexpr auto totalNumbers = 10000000U;
int main() {
std::mt19937 mt(std::random_device{}());
std::uniform_int_distribution<unsigned int> dist(1, std::numeric_limits<unsigned int>::max());
std::uniform_real_distribution<float> floatdist(std::numeric_limits<float>::min(), std::numeric_limits<float>::max());
std::vector<std::pair<std::function<unsigned int()>, std::string>> dataSets = {
{[&] { return dist(mt); }, "Uniform random distribution"},
{[&] { return (dist(mt) / 1000) * 1000; }, "Common value distribution"},
// {[&] { return reinterpret_cast<unsigned int>(floatdist(mt)); }, "Extreme distribution"},
{[] { return 0; }, "All zeros"},
{[] { return std::numeric_limits<unsigned int>::max(); }, "All max int"},
{[] {
static unsigned int upto = 0U;
return ++upto;
}, "Monotomically increasing"},
};
const unsigned int numCores = std::thread::hardware_concurrency();
std::ofstream results("results.csv");
results << totalNumbers << '\n';
for (const auto &dataset : dataSets) {
const auto &generator = dataset.first;
const auto &desc = dataset.second;
BucketSort b;
std::vector<unsigned int> data;
// insert random numbers into the sort object
for (unsigned int i=0; i < totalNumbers; ++i) {
data.push_back(generator());
}
b.numbersToSort = data;
b.sort(numCores); // ensure the cache is fair for each test
// potentially could do i *= 2, not ++i
for (auto currentCores = numCores; currentCores <= numCores; ++currentCores) {
std::cout << "Testing " << desc << " with " << currentCores << " core(s)" << std::endl;
results << desc << ',' << currentCores;
for (auto i = 0U; i < numreps; ++i) {
BucketSort b;
b.numbersToSort = data;
auto start = std::chrono::high_resolution_clock::now();
b.sort(currentCores);
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::high_resolution_clock::now() - start
);
results << ',' << ms.count();
}
results << std::endl;
}
}
}