Ok, I spent a little time with the code. While I really didn't change much, I wanted to make sure we were comparing apples to apples.
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 a faster SQRT-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 a much faster SQRT-Calculation: ", t3, " ms"
print "Dist# = ", dist#
rem Calculate Heuristic
t = timer()
for x = 1 to 100000
Gosub GetHeuristic
next x
t4 = timer()-t
print "Time needed for Heuristic-Calculation: ", t4, " ms"
print "Dist# = ", dist#
rem Calculate Vector
t = timer()
for x = 1 to 100000
Gosub GetVector
next x
t5 = timer()-t
print "Time needed for Sasuke's Vector-Calculation: ", t5, " ms"
print "Dist# = ", dist#
print
print "Press space to run it again, any other key to exit."
sync
rem Get key
repeat : until scancode()=0
repeat
s = scancode()
until s <> 0
if s=57 then goto Restart
end
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
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
The original didn't change the object positions. Not that this makes a big difference for calculating time, but it shows the level of accuracy between the heuristic formula and the others.
I didn't change the code so that the objects could have negative positions, so I'm not really sure what impact that might have.
I'll have to find the thread, Jason, I think you were in that thread weren't you? I may have it backwards, but I could swear that the conclusion was that the sqrt formula was faster.
The reason for moving some things around is so that each subroutine is presented with the same data to work from, and no other commands or processes have to occur for them to do their calculation.
In my tests, the vector and the faster Sqrt functions performed the fastest. If you hit the space to run it about 20 times you'll notice that the two sort of flip-flop between 13-16 and 42-47. On my machine about 6 out of 10 times the sqrt3 function came out 13-16, while the vector function came out at 42-47.
I'm not sure why they flip around, but given that those two are the fastest using either one of them should be your best performance bet. They are close enough in performance that the difference is negligable.
Now if we could substitute a simpler formula for SQRT like was done replacing the X^2 with X*X.