I am doing some image processing on my Beaglebone Black and am interested in the performance gain of using floats vs doubles in my algorithm.
I've tried to devise a simple test for this:
main.c
#define MAX_TEST 10
#define MAX_ITER 1E7
#define DELTA 1E-8
void float_test()
{
float n = 0.0;
for (int i=0; i<MAX_ITER; i++)
{
n += DELTA;
n /= 3.0;
}
}
void double_test()
{
double n = 0.0;
for (int i=0; i<MAX_ITER; i++)
{
n += DELTA;
n /= 3.0;
}
}
int main()
{
for (int i=0; i<MAX_TEST; i++)
{
double_test();
float_test();
}
return 0;
}
ran as:
gcc -Wall -pg main.c -std=c99
./a.out
gprof a.out gmon.out -q > profile.txt
profile.txt:
granularity: each sample hit covers 4 byte(s) for 0.03% of 35.31 seconds
index % time self children called name
<spontaneous>
[1] 100.0 0.00 35.31 main [1]
18.74 0.00 10/10 float_test [2]
16.57 0.00 10/10 double_test [3]
-----------------------------------------------
18.74 0.00 10/10 main [1]
[2] 53.1 18.74 0.00 10 float_test [2]
-----------------------------------------------
16.57 0.00 10/10 main [1]
[3] 46.9 16.57 0.00 10 double_test [3]
-----------------------------------------------
I am not sure if the compiler is optimizing away some of my code or if I am doing enough arithmetic for it to matter. I find it a bit odd that the double_test() is actually taking less time than the float_test().
I've tried switching the order in which the functions are called and they results are still the same. Could somebody explain this to me?