E
Eric Lilja
Hello, consider the following two functions:
/* function foo() */
void foo()
{
float y = 0.0f;
float sum = 0.0f;
for(int i = 0; i < num; ++i)
{
for(int j = 0; j < num; ++j)
{
const float& x = myDataX;
y = myDataY[j];
sum += x + y;
}
}
std::cout << "sum = " << sum << std::endl;
}
/* function bar() */
void bar()
{
float sum = 0.0f;
float y_sum = 0.0f;
for(int y = 0; y < num; ++y)
y_sum += myDataY[y];
for(int x = 0; x < num; ++x)
{
float x_times_num = myDataX[x] * num;
float value = (x_times_num + y_sum);
sum += value;
}
std::cout << "sum = " << sum << std::endl;
}
If myDataX and myDataY are small arrays, both functions produce the correct
sum and bar() seems to be a bit faster (I removed code for timing when
posting). But for large sets of data, bar() produces a too large sum but is
very much faster. Why? Where's the bug in bar()? I have a feeling it has to
do with the fact that I'm using floats and floats don't always behave as one
might expect unless you understand in detail how they're represented
internally. I want to fix bar() so it produces the correct sum for large
data sets, and, hopefully still doing it faster than foo(). I am compiling
with debug info and will all optimizations disabled.
If you see any bugs, have suggestions on how to improve the speed of my
functions, please reply. I need to calculate the sum calculated by foo() and
bar() as fast as possible.
Example data set that works for both:
const int num = 4;
myDataX = new float[num];
myDataY = new float[num];
myDataX[0] = 1;
myDataX[1] = 2;
myDataX[2] = 3;
myDataX[3] = 4;
myDataY[0] = 5;
myDataY[1] = 6;
myDataY[2] = 7;
myDataY[3] = 8;
Example data set (large) where bar() produces an incorrect sum:
const int num = 20000;
myDataX = new float[num];
myDataY = new float[num];
std::fill(myDataX, &myDataX[num-1], 0.1f);
std::fill(myDataY, &myDataY[num-1], 0.1f);
These variables are static globals in my test program.
Thanks for any replies
/ E
/* function foo() */
void foo()
{
float y = 0.0f;
float sum = 0.0f;
for(int i = 0; i < num; ++i)
{
for(int j = 0; j < num; ++j)
{
const float& x = myDataX;
y = myDataY[j];
sum += x + y;
}
}
std::cout << "sum = " << sum << std::endl;
}
/* function bar() */
void bar()
{
float sum = 0.0f;
float y_sum = 0.0f;
for(int y = 0; y < num; ++y)
y_sum += myDataY[y];
for(int x = 0; x < num; ++x)
{
float x_times_num = myDataX[x] * num;
float value = (x_times_num + y_sum);
sum += value;
}
std::cout << "sum = " << sum << std::endl;
}
If myDataX and myDataY are small arrays, both functions produce the correct
sum and bar() seems to be a bit faster (I removed code for timing when
posting). But for large sets of data, bar() produces a too large sum but is
very much faster. Why? Where's the bug in bar()? I have a feeling it has to
do with the fact that I'm using floats and floats don't always behave as one
might expect unless you understand in detail how they're represented
internally. I want to fix bar() so it produces the correct sum for large
data sets, and, hopefully still doing it faster than foo(). I am compiling
with debug info and will all optimizations disabled.
If you see any bugs, have suggestions on how to improve the speed of my
functions, please reply. I need to calculate the sum calculated by foo() and
bar() as fast as possible.
Example data set that works for both:
const int num = 4;
myDataX = new float[num];
myDataY = new float[num];
myDataX[0] = 1;
myDataX[1] = 2;
myDataX[2] = 3;
myDataX[3] = 4;
myDataY[0] = 5;
myDataY[1] = 6;
myDataY[2] = 7;
myDataY[3] = 8;
Example data set (large) where bar() produces an incorrect sum:
const int num = 20000;
myDataX = new float[num];
myDataY = new float[num];
std::fill(myDataX, &myDataX[num-1], 0.1f);
std::fill(myDataY, &myDataY[num-1], 0.1f);
These variables are static globals in my test program.
Thanks for any replies
/ E