-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.cpp
More file actions
182 lines (149 loc) · 6.57 KB
/
benchmark.cpp
File metadata and controls
182 lines (149 loc) · 6.57 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#pragma once
#ifndef BENCHMARK_CPP
#include "src/Sorter.cpp"
#include <concepts>
#include <random>
#include <algorithm>
#include <assert.h>
#include <limits>
#include <chrono>
#include <iostream>
#include <iomanip>
#include <fstream>
template<class T>
class Benchmark {
public:
Benchmark(Sorter<T>* sorting_engine): sorting_engine(sorting_engine), gen(rd()) {
std::string file_name = sorting_engine->get_sort_name() + ".txt";
if constexpr(std::is_integral_v<T>) {
test_sizes = {10, 30, 100, 300, 1000, 3000, 10000, 30000, 100000, 300000, 1000000};
fout = std::ofstream("benchmark_results/Int_" + file_name);
}
else if constexpr(std::is_same_v<T, std::string>) {
test_sizes = {10, 30, 100, 300, 1000, 3000, 10000};
lengths = {1, 10, 100, 1000, 10000};
fout = std::ofstream("benchmark_results/String_" + file_name);
}
}
/// @brief Tests the time of sorting on different times. Prints a nice table to stdin and file as well.
void test() {
if constexpr (std::is_integral_v<T>) {
std::cout << std::setw(23) << sorting_engine->get_sort_name() << std::endl;
std::cout << "|" << std::setw(7) << "N" << "|" << std::setw(15) << "Time taken" << "|" << std::endl;
for (const auto& test_size : test_sizes) {
std::cout << "|" << std::setw(7) << test_size << "|";
if (sorting_engine->get_max_test_size() >= test_size) {
auto result = test_one_size(test_size);
std::cout << std::setw(15) << convert_time(result) << "|" << std::endl;
fout << "N:" << test_size << " " << result << std::endl;
}
else {
std::cout << std::setw(15) << ">5s" << "|" << std::endl;
fout << "N:" << test_size << " " << ">5s" << std::endl;
}
assert(std::is_sorted(test_data.begin(), test_data.end()) && "Array was sorted incorrectly");
}
}
else if constexpr (std::is_same_v<T, std::string>) {
std::cout << std::setw(23) << sorting_engine->get_sort_name() << std::endl;
std::cout << "|" << std::setw(7) << "M \\ N" << "|";
for (auto& test_size : test_sizes) {
std::cout << std::setw(15) << test_size << "|";
}
std::cout << std::endl;
for (auto& string_size : lengths) {
std::cout << "|" << std::setw(7) << string_size << "|";
for (auto& test_size : test_sizes) {
if (sorting_engine->get_max_test_size() >= test_size) {
auto result = test_one_size(test_size, string_size);
std::cout << std::setw(15) << convert_time(result) << "|";
fout << "(N: " << test_size << " M: " << string_size << ") " << result << std::endl;
}
else {
std::cout << std::setw(15) << ">5s" << "|";
fout << "(N: " << test_size << " M: " << string_size << ") " << ">5s" << std::endl;
}
assert(std::is_sorted(test_data.begin(), test_data.end()) && "Array was sorted incorrectly");
}
std::cout << std::endl;
}
}
}
private:
/// @brief Tests the performance of the used sorting engine on a chosen test size
/// @param test_size Testing array size
/// @param string_size In case of elements being strings, the length of strings
/// @param runs Number of runs to average out performance
/// @return Average time of execution
int64_t test_one_size(size_t test_size, size_t string_size = 0, int runs = 10) {
int64_t result = 0;
for (int i = 0; i < runs; i++) {
result += time_test(test_size, string_size);
}
return result / runs;
}
/// @brief Measures the execution time of one run
/// @param test_size Array size
/// @param string_size If elements are strings, the string length
/// @return The time taken by the sorting engine
int64_t time_test(size_t test_size, size_t string_size = 0) {
test_data = generate_test(test_size, string_size);
auto start = std::chrono::high_resolution_clock::now();
sorting_engine->sort(test_data, 0, test_size - 1);
auto end = std::chrono::high_resolution_clock::now();
int64_t result = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count();
//std::cout << result << std::endl;
return result;
}
/// @brief Generates a random array of elements
/// @param test_size Array size
/// @param string_size If the elements are strings, generates strings this size
/// @return Generated test array
std::vector<T> generate_test(size_t test_size, size_t string_size = 0) {
if constexpr(std::is_integral_v<T>) {
T min_value = std::numeric_limits<T>::min();
T max_value = std::numeric_limits<T>::max();
std::uniform_int_distribution dis(min_value, max_value);
std::vector<T> arr(test_size);
for (auto& it : arr) {
it = dis(gen);
}
return arr;
}
else if constexpr(std::is_same_v<T, std::string>) {
std::vector<T> arr(test_size);
std::uniform_int_distribution<int> dis(0, 25);
for (auto &it : arr) {
T current = std::string(string_size, 'a');
for (auto& ch : current) {
ch += dis(gen);
}
it = current;
}
return arr;
}
}
/// @brief Function to convert time to mcs/ms/s depending on the value of time
/// @param time Time in nanoseconds
/// @return String representation of time value
std::string convert_time(int64_t time) const {
if (time < 1000) {
return std::to_string(time) + " ns";
}
else if (time < 1e6) {
return std::to_string(std::round(time / 1e3 * 1e3) / 1000) + " mcs";
}
else if (time < 1e9) {
return std::to_string(std::round(time / 1e6 * 1e6) / 1000000) + " ms";
}
return std::to_string(std::round(time / 1e9 * 1e9) / 1000000000) + " s";
}
std::random_device rd;
std::mt19937 gen;
Sorter<T>* sorting_engine;
std::vector<T> test_data;
std::vector<size_t> test_sizes;
std::vector<size_t> lengths;
std::ofstream fout;
};
#endif