Efficiency question
-
I have the following codes:
double Length0(double x1, double y1, double z1, double x2, double y2, double z2)
{
double dx = x2 - x1;
double dy = y2 - y1;
double dz = z2 - z1;return (dx\*dx + dy\*dy + dz\*dz);
}
double Length1(double x1, double y1, double z1, double x2, double y2, double z2)
{
return ((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1) + (z2 - z1)*(z2 - z1));
}Assume all other conditions are the same, which one (Length0 or Length1) theoretically executes faster? A bit explanation is highly appreciated.
-
I have the following codes:
double Length0(double x1, double y1, double z1, double x2, double y2, double z2)
{
double dx = x2 - x1;
double dy = y2 - y1;
double dz = z2 - z1;return (dx\*dx + dy\*dy + dz\*dz);
}
double Length1(double x1, double y1, double z1, double x2, double y2, double z2)
{
return ((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1) + (z2 - z1)*(z2 - z1));
}Assume all other conditions are the same, which one (Length0 or Length1) theoretically executes faster? A bit explanation is highly appreciated.
With optimisations on, both the same - common sub-expression elimination should reduce
Length1
to the equivalent ofLength0
, except with the variables in registers rather than on the stack. With optimisations off...probablyLength0
is more efficient.Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
I have the following codes:
double Length0(double x1, double y1, double z1, double x2, double y2, double z2)
{
double dx = x2 - x1;
double dy = y2 - y1;
double dz = z2 - z1;return (dx\*dx + dy\*dy + dz\*dz);
}
double Length1(double x1, double y1, double z1, double x2, double y2, double z2)
{
return ((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1) + (z2 - z1)*(z2 - z1));
}Assume all other conditions are the same, which one (Length0 or Length1) theoretically executes faster? A bit explanation is highly appreciated.
probably negligible with modern compilers.
This signature was proudly tested on animals.
-
I have the following codes:
double Length0(double x1, double y1, double z1, double x2, double y2, double z2)
{
double dx = x2 - x1;
double dy = y2 - y1;
double dz = z2 - z1;return (dx\*dx + dy\*dy + dz\*dz);
}
double Length1(double x1, double y1, double z1, double x2, double y2, double z2)
{
return ((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1) + (z2 - z1)*(z2 - z1));
}Assume all other conditions are the same, which one (Length0 or Length1) theoretically executes faster? A bit explanation is highly appreciated.
Below is the assembly code generated by Visual Studio 2008 for both the functions. The code was generated with full optimization.
?Length0@@YANNNNNNN@Z PROC
fld QWORD PTR \_x2$\[esp-4\] fsub QWORD PTR \_x1$\[esp-4\] fld QWORD PTR \_y2$\[esp-4\] fsub QWORD PTR \_y1$\[esp-4\] fld QWORD PTR \_z2$\[esp-4\] fsub QWORD PTR \_z1$\[esp-4\] fld ST(1) fmulp ST(2), ST(0) fld ST(2) fmulp ST(3), ST(0) fxch ST(1) faddp ST(2), ST(0) fmul ST(0), ST(0) faddp ST(1), ST(0) ret 0
?Length0@@YANNNNNNN@Z ENDP
?Length1@@YANNNNNNN@Z PROC
fld QWORD PTR \_x2$\[esp-4\] fsub QWORD PTR \_x1$\[esp-4\] fld QWORD PTR \_y2$\[esp-4\] fsub QWORD PTR \_y1$\[esp-4\] fld QWORD PTR \_z2$\[esp-4\] fsub QWORD PTR \_z1$\[esp-4\] fld ST(1) fmulp ST(2), ST(0) fld ST(2) fmulp ST(3), ST(0) fxch ST(1) faddp ST(2), ST(0) fmul ST(0), ST(0) faddp ST(1), ST(0) ret 0
?Length1@@YANNNNNNN@Z ENDP
Here you will notice that both are exactly the same. The compiler has taken away the responsibility from the programmer about optimizing code. At least a lot of it. :)
«_Superman_» I love work. It gives me something to do between weekends.
-
I have the following codes:
double Length0(double x1, double y1, double z1, double x2, double y2, double z2)
{
double dx = x2 - x1;
double dy = y2 - y1;
double dz = z2 - z1;return (dx\*dx + dy\*dy + dz\*dz);
}
double Length1(double x1, double y1, double z1, double x2, double y2, double z2)
{
return ((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1) + (z2 - z1)*(z2 - z1));
}Assume all other conditions are the same, which one (Length0 or Length1) theoretically executes faster? A bit explanation is highly appreciated.
yellowine wrote:
double Length0(double x1, double y1, double z1, double x2, double y2, double z2) { double dx = x2 - x1; double dy = y2 - y1; double dz = z2 - z1; return (dx*dx + dy*dy + dz*dz); }
You can see no specific improvements with that change there. but on the other hand There would be some improvements with the first case (length0), If the calculations happen inside a loop. just a rough example:
double y1, double z1, double x2, double y2, double z2)
{
double dx = x2 - x1;
double dy = y2 - y1;
double dz = z2 - z1;double result =0.0;
for(int i=xx;i<yx;i++)>
{
result += (dx* i + dy * i + dz * i);
}
return result;
}There it would make sense to keep them out of the loop and see some performance improvements.
He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus