I'm currently benchmarking several algorithms in C code. I recognized the following behavior I cannot explain:
When comparing the execution times of the pow()
and powf()
function of the math.h library, executing the powf()
function is two times slower than pow()
.
I used powf()
with float values only and pow() with double values, so there should not be any implicit type conversion.
I execute the code on a beaglebone black and using gcc to compile it. Currently, I do not use any optimization flags. If using -O3
, the execution times are nearly the same.
Is there an explanation why powf()
is so much slower?
Here is a minimal example of what I did:
#include<time.h>
#include <stdio.h>
#include <math.h>
struct timespec diff_time(struct timespec start, struct timespec end)
{
struct timespec temp;
if ((end.tv_nsec - start.tv_nsec) < 0) {
temp.tv_sec = end.tv_sec - start.tv_sec - 1;
temp.tv_nsec = 1000000000 + end.tv_nsec - start.tv_nsec;
}
else {
temp.tv_sec = end.tv_sec - start.tv_sec;
temp.tv_nsec = end.tv_nsec - start.tv_nsec;
}
return temp;
}
int main() {
struct timespec time1, time2;
double time_diff;
double result=0;
float resultf=0;
double value = 234.2348;
float valuef = 234.2348f;
int j;
int select_switch = 1;
//TIC
clock_gettime(CLOCK_REALTIME, &time1);
if (select_switch == 1) {
for (j = 0; j < 1000; j++)
{
result = pow(value, 2);
}
}
if (select_switch == 2) {
for (j = 0; j < 1000; j++)
{
resultf = powf(valuef, 2.0f);
}
}
if (select_switch == 4) {
for (j = 0; j < 1000; j++)
{
resultf = valuef * valuef;
}
}
if (select_switch == 5) {
for (j = 0; j < 1000; j++)
{
result = value * value;
}
}
/* TOC */
clock_gettime(CLOCK_REALTIME, &time2);
time_diff = diff_time(time1, time2).tv_sec * (1e3) +
(diff_time(time1, time2).tv_nsec) * (1e-6); // in Milli Seconds
printf("%lf", result);
printf("%f", resultf);
}