-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
89 lines (75 loc) · 2.55 KB
/
main.cpp
File metadata and controls
89 lines (75 loc) · 2.55 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
#include <algorithm>
#include <iostream>
#include <chrono>
#include <fstream>
#include <unistd.h>
#include <vector>
#include <random>
#include <string>
#include <cstdlib>
#include <atomic>
// Global atomic counter for heap allocations
std::atomic<size_t> totalAllocatedBytes(0);
void* operator new(std::size_t sz) {
totalAllocatedBytes += sz;
return malloc(sz);
}
void operator delete(void* ptr) noexcept {
free(ptr);
}
long double average(const std::vector<long double>& data) {
if (data.empty()) return 0.0;
long double sum = 0.0;
for (auto x : data) {
sum += x;
}
return sum / data.size();
}
std::string generateRandomString(size_t length) {
const std::string chars =
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"0123456789";
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dist(0, chars.size() - 1);
std::string result;
for (size_t i = 0; i < length; ++i) {
result += chars[dist(gen)];
}
return result;
}
void exportToCSV(std::vector<long double> x, std::vector<long double> y, std::string name, int size) {
std::ofstream file("data.csv");
file << name << "," << size << "\n";
for(size_t i = 0; i < x.size(); ++i) {
file << x[i] << "," << y[i] << "\n";
}
}
template<typename func>
void benchmark(func algoritmen, const std::string& name, int maxLength, int repetitions = 100) {
std::vector<long double> times;
std::vector<long double> memorys;
for(int length = 0; length < maxLength; length++) {
size_t maxMemoryBytes = 0;
std::vector<long double> iterationTimes;
std::string text = generateRandomString(length);
for(int i = 0; i < repetitions; i++) {
totalAllocatedBytes = 0;
auto startTime = std::chrono::steady_clock::now();
algoritmen(text);
auto endTime = std::chrono::steady_clock::now();
if (totalAllocatedBytes > maxMemoryBytes) {
maxMemoryBytes = totalAllocatedBytes;
}
std::chrono::duration<long double, std::milli> duration = endTime - startTime;
if (duration.count() > 0.0 && duration.count() < 1000.0) { // filter absurd spikes
iterationTimes.push_back(duration.count());
}
}
times.push_back(average(iterationTimes));
memorys.push_back(static_cast<long double>(maxMemoryBytes / 1024.0));
}
exportToCSV(times, memorys, name, maxLength);
system("python3 plot.py");
}