-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFLOPperformanceTest.cc
50 lines (39 loc) · 1.31 KB
/
FLOPperformanceTest.cc
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
#include <chrono>
#include <cstdlib>
#include <fstream>
#include <iostream>
using namespace std;
using namespace chrono;
int main() {
ofstream file("results.csv");
file << "Iterations,Operations,Time,OperationsPerSecond,CPUFrequency\n";
// Loop through different numbers of iterations
for (long long num_iterations = 1000000; num_iterations <= 1000000000;
num_iterations *= 10) {
double a = 1.0, b = 1.0000001, c = 1.00000001;
volatile double result = 0.0; // volatile to prevent optimization
auto start = high_resolution_clock::now();
for (long long i = 0; i < num_iterations; ++i) {
result += a * b;
result -= c;
result *= a;
result /= b;
}
auto end = high_resolution_clock::now();
duration<double> elapsed = end - start;
// Get CPU frequency
system("sysctl -n machdep.cpu.brand_string > cpuinfo.txt");
ifstream cpuinfo("cpuinfo.txt");
string cpufreq;
getline(cpuinfo, cpufreq);
cpuinfo.close();
system("rm cpuinfo.txt");
// Calculate operations per second
double ops_per_second =
num_iterations * 4 / elapsed.count(); // 4 FLOPs per iteration
file << num_iterations << "," << num_iterations * 4 << ","
<< elapsed.count() << "," << ops_per_second << "," << cpufreq << "\n";
}
file.close();
return 0;
}