I want to compare the speed of functions in C. This is my approach.
I measure the time of each implementation by making the calculation on the input array a certain number of times and adding up the time of each iteration using the monotonic clock and clock_gettime.
totalTime = 0.0;
for (int i = 0; i < ITERATIONS; ++i) {
startTime = (float)clock()/CLOCKS_PER_SEC;
doFun(input, output);
endTime = (float)clock()/CLOCKS_PER_SEC;
timeElapsed = endTime - startTime;
totalTime += timeElapsed;
}
printf("Time: %f \n", totalTime);
-O0
Native: 26.296245
Quick: 34.653388
SSE: 21.701504
-O1
Native: 11.036241
Quick: 7.885706
SSE: 2.043576
-O2
Native: 0.000776
Quick: 0.000795
SSE: 0.000761
I am really unsure if this is even the right approach to compare the algorithm and alternatives.
Especially for -O0
I don't understand how the algorithm is slower than the native function although it should be up to 4 times faster and the SSE variant again ideally 4 times faster than that.
At what optimization level should I compare the algorithms to have meaningful results and why do I not get the expected times at -O0
?