As you'd expect, using the SQRT command (per David T's snippet) is fractionally faster than setting up a DBPro Vector3 and applying the length vector command to it (per my code). This is because the length vector command will itself perform a square root calculation. Both methods ultimately have to use pythagorus' equation.
See:
sync on
sync rate 0
set display mode 1024, 768, 32
temp1 = timer()
for i = 1 to 1000000
number# = Dist(1, 2, 3, 4, 5, 6)
next i
temp2 = timer()
result1 = temp2 - temp1
text 1024 / 2, 768 / 2, "Time1 = " + str$(result1)
sync
wait key
temp1 = timer()
for i = 1 to 1000000
number# = threeD_distance(1, 2, 3, 4, 5, 6)
next i
temp2 = timer()
result2 = temp2 - temp1
text 1024 / 2, 768 / 2, "Time1 = " + str$(result1)
text 1024 / 2, (768 / 2) + 20, "Time2 = " + str$(result2)
sync
wait key
Function Dist(Ob1x#, Ob2x#, Ob1y#, Ob2y#, Ob1z#, Ob2z#)
Dist# = Sqrt((Ob1x#-Ob2x#)^2+(Ob1y#-Ob2y#)^2+(Ob1z#-Ob2z#)^2)
Endfunction dist#
FUNCTION threeD_distance(x1# AS float, x2# AS float, y1# AS float, y2# AS float, z1# AS float, z2# AS float)
temp = 1
null = make vector3(temp)
set vector3 temp, x1# - x2#, y1# - y2#, z1# - z2#
length_vector = length vector3(temp)
null = delete vector3(temp)
ENDFUNCTION length_vector#
My computer's results for this are:
SQRT route: 2671
Length vector route: 2750
However, the length vector command option will be very considerably faster when you already have vectors created. This is because you no longer have the overhead of making a vector3 each time.
See, for example:
sync on
sync rate 0
set display mode 1024, 768, 32
temp = 1
null = make vector3(temp)
set vector3 temp, 1 - 2, 3 - 4, 5 - 6
temp1 = timer()
for i = 1 to 1000000
Dist# = Sqrt((1 - 2) ^ 2 + (3 - 4) ^ 2 + (5 - 6) ^ 2)
next i
temp2 = timer()
result1 = temp2 - temp1
text 1024 / 2, 768 / 2, "Time1 = " + str$(result1)
sync
wait key
temp1 = timer()
for i = 1 to 1000000
length_vector# = length vector3(temp)
next i
temp2 = timer()
result2 = temp2 - temp1
text 1024 / 2, 768 / 2, "Time1 = " + str$(result1)
text 1024 / 2, (768 / 2) + 20, "Time2 = " + str$(result2)
sync
wait key
null = delete vector3(temp)
In this situation my results are:
SQRT route: 3511
Length vector route: 120
So thats quite a dramatic difference.
The one thing that does puzzle me with this is why the SQRT route seems to take longer when the function call is omitted. I would have thought the converse should be true.
Cheers
Philip
What do you mean, bears aren't supposed to wear hats and a tie? P1.3ghz / 384 megs / GeForce MX 5200 128meg / WinXP home