@zipit said in What is Vector.GetLengthSquared() really meant for?:
My major point was that there are certain programming assumptions (multiplication is better than division, never take the square root if avoidable, cubic complexity is uncomputable, etc.) that should be taken with a grain of salt due to the fact that they rely on a certain "state" of hardware. I.e. all these three do not really hold true anymore to the extent they once did. (...)
That goes without saying... ultimately, any effort towards optimization needs to be checked for effectivity.
Nevertheless, I was curious and abused a plugin of mine to execute some timer checks in C++, just for comparison (code excerpt only):
using namespace std::chrono;
Vector v(100.0, 100.0, 100.0);
float f;
milliseconds ms1, ms2, diff;
ms1 = duration_cast<milliseconds>(
system_clock::now().time_since_epoch()
);
for (int i = 0; i < 100000000; i++)
{
f = 0; // v.GetLength();
}
ms2 = duration_cast<milliseconds>(
system_clock::now().time_since_epoch()
);
diff = ms2 - ms1;
GePrint(maxon::String("Time counter Empty:") + maxon::String::IntToString(diff.count()));
ms1 = duration_cast<milliseconds>(
system_clock::now().time_since_epoch()
);
for (int i = 0; i < 100000000; i++)
{
f = v.GetLength();
}
ms2 = duration_cast<milliseconds>(
system_clock::now().time_since_epoch()
);
diff = ms2 - ms1;
GePrint(maxon::String("Time counter GetLength:") + maxon::String::IntToString(diff.count()));
ms1 = duration_cast<milliseconds>(
system_clock::now().time_since_epoch()
);
for (int i = 0; i < 100000000; i++)
{
f = v.GetSquaredLength();
}
ms2 = duration_cast<milliseconds>(
system_clock::now().time_since_epoch()
);
diff = ms2 - ms1;
GePrint(maxon::String("Time counter GetSquaredLength:") + maxon::String::IntToString(diff.count()));
Switching off all optimizations, I get (for multiple button presses):
Time counter Empty:185
Time counter GetLength:921
Time counter GetSquaredLength:228
Time counter Empty:184
Time counter GetLength:922
Time counter GetSquaredLength:228
Time counter Empty:183
Time counter GetLength:921
Time counter GetSquaredLength:228
Time counter Empty:183
Time counter GetLength:922
Time counter GetSquaredLength:228
Time counter Empty:185
Time counter GetLength:921
Time counter GetSquaredLength:228
Time counter Empty:183
Time counter GetLength:921
Time counter GetSquaredLength:227
That is far more like what I expected (double so if you consider that the loop with a constant assignment already takes 185ms).
Considering that I had to up the loop count to a hundred million to get measurable results, it is practically guaranteed that any difference between GetLength and GetLengthSquared in the Python sample is drowned in the Python interpreter's overhead, and any result from my initial tests must be attributed to sheer randomness.