So, I decided to write a simple, näive code snippet to verify and take my own conclusions. Here it follows:
Code: Select all
/**
* rectest.c
**/
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
clock_t start, end;
float *values = NULL;
int valuesLength = 0;
float averageWithLoop (float * myArray, int size)
{
float average, sumValues = 0.0f;
int i;
for (i = 0; i < size; i++)
{
sumValues += myArray[i];
}
average = ((float)sumValues/size);
return average;
}
float averageWithRecursion (float *myArray, int size)
{
if (size == 2)
{
return ((float)(myArray[0] + myArray[1])/2);
}
return ((averageWithRecursion (myArray, (int)(size / 2)) + averageWithRecursion (&myArray[(int)(size / 2)], (int)(size / 2)))/2);
}
void doAverage (int recursive)
{
if (recursive)
{
printf ("Average (recursive) = %f\n", averageWithRecursion (values, valuesLength));
}
else
{
printf ("Average (loop) = %f\n", averageWithLoop (values, valuesLength));
}
}
int main (int argc, char **argv)
{
int i;
valuesLength = 16 * 1024 * 1024;
values = (float *)malloc (sizeof (float) * valuesLength);
if (!values)
{
printf ("Unable to alloc %i float values\n", valuesLength);
exit (-1);
}
for (i = 0; i < valuesLength; i++)
{
values[i] = ((float)rand()/RAND_MAX);
}
start = clock();
doAverage ((argc > 1) ? 1 : 0);
end = clock ();
printf ("Elapsed clock ticks: %i\n", end - start);
return 0;
}
So, I'd like to hear from you guys what do you think about the subject, and how could I improve/fix the recursive code above so it can perform at least as good as the loop version ?
EDIT:Regarding the precision loss, I managed to fix it simply working with double precision variables and returns in both functions. Still, the performance loss remains unchanged.