I've been doing some experiments on speed testing for ranges.
The three functions I tested are:
The Test
Compare the range between point A & point B defined as a 2D co-ordinate.
range()
This is a 1D testing method, so here I have simply added the results of the 2 axis together in order to compare it. Results are not terribly accurate but on the whole fairly speedy.
range3D()
Traditional square root method of calculating range. I noticed some alarming innacuracies with large numbers (100,000) or so making me wonder if I typed the algorythm in wrong? Anyway the results are varied, normally the slowest and sometimes significantly slower than the other methods.
fastRange3D
I wrote this especially for the test to see how accurately I could calculate the range of a 2D co-ordinate using 1D maths. I was shocked to see this return the most accurate results and it was consistently the fastest routine! It will now be my standard range calculation method from now on!
The Test Code
sync on
cls 0
text 20,20,"PRESS KEY TO START"
sync : sync
wait key
rem position A
aX=50 : aZ=150
rem position B
bX=0 : bZ=500
rem Intensity of test
testAccuracy#=5000000
cls 0 : sync
start=timer()
for speed=1 to testAccuracy#
totalRange=range(aX,bX)+range(aZ,bZ)
next speed
finish=timer()
cls 0
report(start,finish,testAccuracy#,totalRange)
cls 0 : sync
start=timer()
for speed=1 to testAccuracy#
totalRange=range3D(aX,aZ,bX,bZ)
next speed
finish=timer()
report(start,finish,testAccuracy#,totalRange)
cls 0 : sync
start=timer()
for speed=1 to testAccuracy#
totalRange=fastRange3D(aX,aZ,bX,bZ)
next speed
finish=timer()
report(start,finish,testAccuracy#,totalRange)
end
`* The different range commands
function fastRange3D(aX,aZ,bX,bZ)
rngX#=abs(aX-bX)
rngZ#=abs(aZ-bZ)
if rngX#>rngZ#
result#=rngX#+((rngZ#/rngX#)*rngZ#)
else
result#=rngZ#+((rngX#/rngZ#)*rngX#)
endIf
endfunction result#
function Range(val1,val2)
result=abs(val1-val2)
endfunction result
function Range3D(aX,aZ,bX,bZ)
result=sqrt(((aX-bX)^2)+((aZ-bZ)^2))
endfunction result
`* Report Results
function report(start,finish,testAccuracy#,totalRange)
calculation#=finish-start
calculation#=calculation#/testAccuracy#
text 20,20,str$(testAccuracy#)+" cycles took: "+str$(finish-start)
text 20,40,"Each Calculation: "+str$(calculation#)
text 20,60,"Tested Result: "+str$(totalRange)
sync : sync
wait key
endfunction
Pneumatic Dryll, Outrageous epic cleric of EQ/Xev
God made the world in 7 days, but we're still waiting for the patch.