-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use Vector:LengthSqr() instead of Vector:Length() #28
Comments
Can you (or anyone) provide a benchmark proving that it would be more efficient? Squared distance being so much more efficient than normal distance that it should be changed everywhere is probably the biggest misunderstanding around gmod devs. I wouldn't necessarily be against changing it here if done properly, since it wouldn't affect code complexity that much, but oh boy does everyone like suggesting this "optimization" without actually testing if it has any impact first. |
The benchmark below shows a very minimal improvement, however the execution time of the square root calculation ultimately depends on the input given. local benchmark
local vec = VectorRand() * 1000
local getLength = vec.Length
local getLengthSqr = vec.LengthSqr
print("Benchmark data : ", vec, "\n")
do
benchmark = SysTime()
for i = 1, 2e5 do end
print("Baseline : ", SysTime() - benchmark)
end
do
benchmark = SysTime()
for i = 1, 2e5 do
getLength(vec)
end
print("Vector:Length() : ", SysTime() - benchmark)
end
do
benchmark = SysTime()
for i = 1, 2e5 do
getLengthSqr(vec)
end
print("Vector:LengthSqr() : ", SysTime() - benchmark)
end
print("\n") Results (after 5 runs):
|
Thanks! So ~5% improvement (or very roughly 10000th of a millisecond per call). I wouldn't mind the change here, since it's very localized and requires only two lines changed. It is quite a micro-optimization, however. I guess crossing the C++ boundary is the source of slowness here. |
This would be more efficient than calculating a square root to compare against.
imgui/imgui.lua
Line 115 in 9219cb2
The text was updated successfully, but these errors were encountered: