-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBenchmark Program
More file actions
235 lines (205 loc) · 9.25 KB
/
Benchmark Program
File metadata and controls
235 lines (205 loc) · 9.25 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include <iostream>
#include <chrono>
#include <vector>
#include <fstream>
#include <string>
#include <map>
#include <iomanip>
#include <cmath>
#include <algorithm>
// Function declarations for all benchmark operations
double integer_benchmark();
double float_benchmark();
double memory_benchmark();
double disk_benchmark_1();
double disk_benchmark_2();
int main() {
// Initialize program and display welcome messages
std::cout << "Starting benchmarks..." << std::endl;
std::cout << "Benchmark results will be displayed here" << std::endl;
git commit -m "Added program initialization and welcome messages"
// Create and populate results container with benchmark data
std::map<std::string, double> results;
results["Integer Benchmark"] = integer_benchmark();
results["Floating Point Benchmark"] = float_benchmark();
results["Memory Benchmark"] = memory_benchmark();
results["Hard Drive Benchmark 1"] = disk_benchmark_1();
results["Hard Drive Benchmark 2"] = disk_benchmark_2();
git commit -m "Implemented benchmark execution and results collection"
// Format and display benchmark results with extra spacing
std::cout << "\n\n\nBenchmark Results:" << std::endl;
std::cout << "----------------------------------------" << std::endl;
for (const auto& [benchmark, time_taken] : results) {
std::cout << "\n\n" << benchmark << ": " << std::fixed << std::setprecision(2) << time_taken << " seconds" << std::endl;
std::cout << "----------------------------------------" << std::endl;
}
git commit -m "Added results formatting and display"
return 0;
}
double integer_benchmark() {
// Start timing the integer operations
auto start_time = std::chrono::high_resolution_clock::now();
git commit -m "Initialized timing for integer benchmark"
// Perform addition operations (10^10 additions)
volatile int result = 0; // Use volatile to prevent optimization
const int chunk_size = 1000000; // Process in chunks of 1 million
for (int chunk = 0; chunk < 10000; chunk++) { // 10000 chunks × 1 million = 10^10
for (int i = 0; i < chunk_size; i++) {
result += 1;
}
}
git commit -m "Implemented integer addition benchmark with chunking"
// Perform multiplication operations (5 × 10^9 multiplications)
result = 1;
for (int chunk = 0; chunk < 5000; chunk++) { // 5000 chunks × 1 million = 5 × 10^9
for (int i = 0; i < chunk_size; i++) {
result *= 2;
}
}
git commit -m "Implemented integer multiplication benchmark with chunking"
// Perform division operations (2 × 10^9 divisions)
result = 1 << 30;
for (int chunk = 0; chunk < 2000; chunk++) { // 2000 chunks × 1 million = 2 × 10^9
for (int i = 0; i < chunk_size; i++) {
result /= 2;
}
}
git commit -m "Implemented integer division benchmark with chunking"
// Calculate and return execution time
auto end_time = std::chrono::high_resolution_clock::now();
return std::chrono::duration<double>(end_time - start_time).count();
git commit -m "Completed integer benchmark timing calculation"
}
double float_benchmark() {
// Start timing the floating-point operations
auto start_time = std::chrono::high_resolution_clock::now();
git commit -m "Initialized timing for float benchmark"
// Perform addition operations (10^10 additions)
volatile double result = 0.0; // Use volatile to prevent optimization
const int chunk_size = 1000000; // Process in chunks of 1 million
for (int chunk = 0; chunk < 10000; chunk++) { // 10000 chunks × 1 million = 10^10
for (int i = 0; i < chunk_size; i++) {
result += 1.0;
}
}
git commit -m "Implemented float addition benchmark with chunking"
// Perform multiplication operations (5 × 10^9 multiplications)
result = 1.0;
for (int chunk = 0; chunk < 5000; chunk++) { // 5000 chunks × 1 million = 5 × 10^9
for (int i = 0; i < chunk_size; i++) {
result *= 2.0;
}
}
git commit -m "Implemented float multiplication benchmark with chunking"
// Perform division operations (2 × 10^9 divisions)
result = std::pow(2.0, 30);
for (int chunk = 0; chunk < 2000; chunk++) { // 2000 chunks × 1 million = 2 × 10^9
for (int i = 0; i < chunk_size; i++) {
result /= 2.0;
}
}
git commit -m "Implemented float division benchmark with chunking"
// Calculate and return execution time
auto end_time = std::chrono::high_resolution_clock::now();
return std::chrono::duration<double>(end_time - start_time).count();
git commit -m "Completed float benchmark timing calculation"
}
double memory_benchmark() {
// Start timing the memory operations
auto start_time = std::chrono::high_resolution_clock::now();
git commit -m "Initialized timing for memory benchmark"
// Initialize array and get pointer for faster access
const size_t chunk_size = 1000000; // Process in chunks of 1 million
const size_t total_size = 5000000000; // 5 × 10^9 elements
std::vector<int> arr(chunk_size, 0);
volatile int32_t* data = arr.data(); // Use volatile to prevent optimization
git commit -m "Set up memory array and pointer for benchmark"
// Perform read and write operations in chunks
for (size_t chunk = 0; chunk < total_size / chunk_size; chunk++) {
// Read operations
for (size_t i = 0; i < chunk_size; i++) {
volatile int temp = data[i]; // Use volatile to prevent optimization
(void)temp; // Prevent unused variable warning
}
git commit -m "Implemented memory read operations in chunks"
// Write operations
for (size_t i = 0; i < chunk_size; i++) {
data[i] = 1;
}
}
git commit -m "Implemented memory write operations in chunks"
// Calculate and return execution time
auto end_time = std::chrono::high_resolution_clock::now();
return std::chrono::duration<double>(end_time - start_time).count();
git commit -m "Completed memory benchmark timing calculation"
}
double disk_benchmark_1() {
// Start timing the disk operations
auto start_time = std::chrono::high_resolution_clock::now();
git commit -m "Initialized timing for first disk benchmark"
// Create and write initial test file (10^9 bytes) in chunks
std::ofstream outFile("test_file_1.bin", std::ios::binary);
const size_t chunk_size = 1000000; // 1MB chunks
std::vector<char> zeros(chunk_size, '0');
for (int i = 0; i < 1000; i++) { // 1000 chunks × 1MB = 1GB
outFile.write(zeros.data(), zeros.size());
}
outFile.close();
git commit -m "Created initial test file with zeros in chunks"
// Perform read operations (100 bytes at a time)
std::ifstream inFile("test_file_1.bin", std::ios::binary);
std::vector<char> buffer(100);
while (inFile.read(buffer.data(), 100)) {
// Skip data processing
}
inFile.close();
git commit -m "Implemented file read operations in 100-byte chunks"
// Perform write operations (100 bytes at a time)
outFile.open("test_file_1.bin", std::ios::binary);
std::vector<char> ones(100, '1');
for (int i = 0; i < 10000000; i++) { // 10^9 bytes total
outFile.write(ones.data(), ones.size());
}
outFile.close();
git commit -m "Implemented file write operations in 100-byte chunks"
// Clean up and calculate execution time
std::remove("test_file_1.bin");
auto end_time = std::chrono::high_resolution_clock::now();
return std::chrono::duration<double>(end_time - start_time).count();
git commit -m "Completed first disk benchmark timing calculation"
}
double disk_benchmark_2() {
// Start timing the disk operations
auto start_time = std::chrono::high_resolution_clock::now();
git commit -m "Initialized timing for second disk benchmark"
// Create and write initial test file (10^9 bytes) in chunks
std::ofstream outFile("test_file_2.bin", std::ios::binary);
const size_t chunk_size = 1000000; // 1MB chunks
std::vector<char> zeros(chunk_size, '0');
for (int i = 0; i < 1000; i++) { // 1000 chunks × 1MB = 1GB
outFile.write(zeros.data(), zeros.size());
}
outFile.close();
git commit -m "Created initial test file with zeros in chunks"
// Perform read operations (10000 bytes at a time)
std::ifstream inFile("test_file_2.bin", std::ios::binary);
std::vector<char> buffer(10000);
while (inFile.read(buffer.data(), 10000)) {
// Skip data processing
}
inFile.close();
git commit -m "Implemented file read operations in 10000-byte chunks"
// Perform write operations (10000 bytes at a time)
outFile.open("test_file_2.bin", std::ios::binary);
std::vector<char> ones(10000, '1');
for (int i = 0; i < 100000; i++) { // 10^9 bytes total
outFile.write(ones.data(), ones.size());
}
outFile.close();
git commit -m "Implemented file write operations in 10000-byte chunks"
// Clean up and calculate execution time
std::remove("test_file_2.bin");
auto end_time = std::chrono::high_resolution_clock::now();
return std::chrono::duration<double>(end_time - start_time).count();
git commit -m "Completed second disk benchmark timing calculation"
}