Here's the code I submitted in the other thread to see if we could prove out the question of which method was faster.
The "Simple Distance" formula was consistently faster.
Now, one might say "Of course it's faster, you aren't getting the sqrt until after the timed loop.", however, that's the point of the exercise. If you need a distance then you can get it any time, if you need to compare the distance to a known value, then in your comparison you multiply the value by itself and compare it with the distance result. No sqrt needed, which speeds it up considerably.
The savings is of course just a few milliseconds, but if you're doing that comparison even a few hundred times each loop, it adds up quick.
rem Create objects
randomize timer()
backdrop off
null = make vector3(1)
global dist# as float
make object cube 1, 10
make object cube 2, 10
Restart:
position object 1, rnd(10000) , rnd(10000) , rnd(10000)
position object 2, rnd(10000) , rnd(10000) , rnd(10000)
x1# = object position x(1): x2# = object position x(2)
y1# = object position y(1): y2# = object position y(2)
z1# = object position z(1): z2# = object position z(2)
cls
rem Calculate SQRT
t = timer()
for x = 1 to 100000
Gosub GetSQRT
next x
t1 = timer()-t
print "Time needed for SQRT-Calculation: ", t1, " ms"
print "Dist# = ", dist#
rem Calculate SQRT 2
t = timer()
for x = 1 to 100000
Gosub GetSQRT2
next x
t2 = timer()-t
print "Time needed for SQRT2-Calculation: ", t2, " ms"
print "Dist# = ", dist#
rem Calculate SQRT 3
t = timer()
for x = 1 to 100000
Gosub GetSQRT3
next x
t3 = timer()-t
print "Time needed for SQRT3-Calculation: ", t3, " ms"
print "Dist# = ", dist#
rem Calculate SQRT 4
t = timer()
for x = 1 to 100000
Gosub GetSQRT4
next x
t4 = timer()-t
print "Time needed for Simple Distance: ", t4, " ms"
print "Dist# = ", sqrt(dist#)
rem Calculate Heuristic
t = timer()
for x = 1 to 100000
Gosub GetHeuristic
next x
t5 = timer()-t
print "Time needed for Heuristic-Calculation: ", t5, " ms"
print "Dist# = ", dist#
rem Calculate Vector
t = timer()
for x = 1 to 100000
Gosub GetVector
next x
t6 = timer()-t
print "Time needed for Sasuke's Vector-Calculation: ", t6, " ms"
print "Dist# = ", dist#
print
print "Press space to run it again, any other key to exit."
sync
wait 500
goto Restart
GetSQRT:
dist# = sqrt(((x1# - x2#)^2) + ((y1# - y2#)^2) + ((z1# - z2#)^2))
Return
GetSQRT2:
x# = x1# - x2#
y# = y1# - y2#
z# = z1# - z2#
dist# = sqrt(x#^2+y#^2+z#^2)
Return
GetSQRT3:
x# = x1# - x2#
y# = y1# - y2#
z# = z1# - z2#
dist# = sqrt(x#*x# + y#*y# + z#*z#)
Return
GetSQRT4:
x# = x1# - x2#
y# = y1# - y2#
z# = z1# - z2#
dist# = (x#*x# + y#*y# + z#*z#)
Return
GetHeuristic:
dist# = Abs(x1# - x2#) + Abs(y1# - y2#) + Abs(z1# - z2#)
Return
GetVector:
set vector3 1, x1# - x2#, y1# - y2#, z1# - z2#
dist# = length vector3(1)
Return