V
velthuijsen
I have a function that before I modified it took around 13.75 seconds
to complete after the modification it took .325 seconds to complete.
the function header:
(Point **Input, size_t InputSize, Point *Output, size_t OutputSize);
Point is a simple x,y,z coordinate structure (3 doubles).
TheDistances is an array containing room for InputSize doubles.
Most of the time consumed (99%+) is in the following for loops:
for (size_t i = 0; i < OutputSize; ++i)
{
for (size_t j = 0; j < OutputSize; ++j)
{
for (size_t k = 0; k < InputSize; ++k)
{
TheDistances[k] = sqrt ( pow(TempPoint.x - (*Input)[k].x,2 ) +
pow(TempPoint.y - (*Input)[k].y, 2) );
if (TheDistances[k] != 0)
{
TheDistances[k] = (pow( TheDistances[k],2) *
(log(TheDistances[k])-1));
}
}
} // for (int j = 0; j < OutputSize; ++j)
} // for (int i = 0; i < OutputSize; ++i)
This takes roughly 13.75 seconds.
By copying the values in Input to Point* InputCopy and then
replacing the innerloop with
TheDistances[k] = sqrt ( pow(TempPoint.x - InputCopy[k].x,2 ) +
pow(TempPoint.y - InputCopy[k].y, 2) );
I get the speed up to roughly 0.325 seconds.
What am I missing here that i get this efficiency increase?
to complete after the modification it took .325 seconds to complete.
the function header:
(Point **Input, size_t InputSize, Point *Output, size_t OutputSize);
Point is a simple x,y,z coordinate structure (3 doubles).
TheDistances is an array containing room for InputSize doubles.
Most of the time consumed (99%+) is in the following for loops:
for (size_t i = 0; i < OutputSize; ++i)
{
for (size_t j = 0; j < OutputSize; ++j)
{
for (size_t k = 0; k < InputSize; ++k)
{
TheDistances[k] = sqrt ( pow(TempPoint.x - (*Input)[k].x,2 ) +
pow(TempPoint.y - (*Input)[k].y, 2) );
if (TheDistances[k] != 0)
{
TheDistances[k] = (pow( TheDistances[k],2) *
(log(TheDistances[k])-1));
}
}
} // for (int j = 0; j < OutputSize; ++j)
} // for (int i = 0; i < OutputSize; ++i)
This takes roughly 13.75 seconds.
By copying the values in Input to Point* InputCopy and then
replacing the innerloop with
TheDistances[k] = sqrt ( pow(TempPoint.x - InputCopy[k].x,2 ) +
pow(TempPoint.y - InputCopy[k].y, 2) );
I get the speed up to roughly 0.325 seconds.
What am I missing here that i get this efficiency increase?