Hello everyone,
I have just written some code that measures a system's FLOPS, but before I wrap it in a class and make a GUI for it, I would like to know what you guys think of it, if you can see any bugs and/or if you got any suggestions for me.
Basically, what I'm doing is I run an empty loop n times, record how long that took, then I run a loop with two floating point operations n times also recording the time it took. I need to run an empty loop because now I know how much of the time is spent doing the looping stuff, like that I can extract how much time the floating point operations took, from which I can calculate the FLOPS.
Here's the code:
#include <iostream>
#include <string>
#include <ctime>
// how many times the loops are run, the higher this number
// is, the longer it takes, but the more accurate it gets.
// max is 4294967295 (cause I'm using uint32_t)
#define LOOP_REPS 4294967295
using namespace std;
int main(int argc, char *argv[]) {
cout.setf(ios_base::fixed); // shows decimals in the output
cout << "loop_reps: " << LOOP_REPS << endl;
// reference loop
clock_t rl_start = clock();
// loop index is volatile so that the empty loop isn't optimized away
for(volatile uint32_t rl_index = 0; rl_index < LOOP_REPS; ++rl_index) {
// empty loop - just to calculate how much time an empty loop needs
}
clock_t rl_end = clock();
double rl_time = difftime(rl_end, rl_start) / CLOCKS_PER_SEC;
// output the time the reference loop took
cout << "cl_time: " << rl_time << endl;
// flops loop
volatile float a = 1.5;
volatile float b = 1.6;
clock_t fl_start = clock();
for(volatile uint32_t fl_index = 0; fl_index < LOOP_REPS; ++fl_index) {
a *= b; // multiplication operation
b += a; // addition operation
}
clock_t fl_end = clock();
double fl_time = difftime(fl_end, fl_start) / CLOCKS_PER_SEC;
unsigned long flops = LOOP_REPS / ((fl_time - rl_time) / 2);
cout << "fl_time: " << fl_time << endl;
cout << "flops: " << flops << endl;
}
The is how the output should look like:
xfbs@remus:~/Dropbox/projects/C++/FlopsTest> ./flops
loop_reps: 4294967295
cl_time: 11.955750
fl_time: 30.865851
flops: 454251121
Cheers, xfbs