-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.cpp
More file actions
105 lines (89 loc) · 3.04 KB
/
benchmark.cpp
File metadata and controls
105 lines (89 loc) · 3.04 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
#include <iostream>
#include <cmath>
#include <fstream>
#include <vector>
#include <random>
#include <filesystem>
#include <chrono>
#include <algorithm>
#include <functional>
class Benchmarks {
private:
std::vector<uint8_t> data;
public:
// Constructor for initializing the data array with 100 MB
Benchmarks() : data(100 * 1024 * 1024) {
Setup();
}
// Setup method to initialize data with random bytes
void Setup() {
std::mt19937 generator(1729);
std::uniform_int_distribution<int> distribution(0, 255);
for (auto &byte : data) {
byte = static_cast<uint8_t>(distribution(generator));
}
}
// CPU benchmark: calculates an approximation of π using a series
double Cpu() {
double pi = 0.0;
for (int i = 1; i <= 500'000'000; ++i) {
pi += 1.0 / (i * i);
}
pi = std::sqrt(pi * 6);
return pi;
}
// Disk benchmark: write and delete a temporary file multiple times
void Disk() {
for (int i = 0; i < 10; ++i) {
std::string fileName = std::filesystem::temp_directory_path() / "tempfile.bin";
std::ofstream file(fileName, std::ios::binary);
file.write(reinterpret_cast<const char*>(data.data()), data.size());
file.close();
std::filesystem::remove(fileName);
}
}
// Memory benchmark: perform random access and sum values in the data array
int Memory() {
std::mt19937 generator(1729);
std::uniform_int_distribution<size_t> distribution(0, data.size() - 1);
int sum = 0;
for (int i = 0; i < 20'000'000; ++i) {
sum += data[distribution(generator)];
}
return sum;
}
};
void runBenchmark(const std::string& name, std::function<void()> func) {
constexpr int NUM_RUNS = 10;
std::vector<double> times;
times.reserve(NUM_RUNS);
for (int i = 0; i < NUM_RUNS; ++i) {
auto start = std::chrono::high_resolution_clock::now();
func();
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed = end - start;
times.push_back(elapsed.count());
}
double minTime = *std::min_element(times.begin(), times.end());
double maxTime = *std::max_element(times.begin(), times.end());
double avgTime = std::accumulate(times.begin(), times.end(), 0.0) / NUM_RUNS;
double variance = 0.0;
for (const auto& t : times) {
variance += (t - avgTime) * (t - avgTime);
}
variance /= NUM_RUNS;
double stdDev = std::sqrt(variance);
std::cout << name << ","
<< minTime << ","
<< maxTime << ","
<< avgTime << ","
<< stdDev << "\n";
}
int main() {
Benchmarks benchmarks;
std::cout << "benchmark_name, min time, max time, avg_time, std_dev\n";
runBenchmark("CPU", [&benchmarks]() { benchmarks.Cpu(); });
runBenchmark("Disk", [&benchmarks]() { benchmarks.Disk(); });
runBenchmark("Memory", [&benchmarks]() { benchmarks.Memory(); });
return 0;
}