Distance

Blitz3D Forums/Blitz3D Programming/Distance

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

I tried taking out the sqr operation but it didn't really seem to speed up at all.

You're not in debug mode are you? It's the inline powering that should get you the lion's share of the speed increase -- Jasu's suggestion is the one I'd go for.

Is BlitzMAX any faster or are there any other Basic languages that are faster?

That's an interesting question so I did the following test...

B3D Version:
SeedRnd(MilliSecs())
Const SECS_TO_DELAY% = 10
Const CALCS_TO_PERFORM% = 100000000

Graphics 800,600,0,2
SetBuffer BackBuffer()

StartTime% = 0
EndTime% = 0
Calculations% = 0
Distance% = 0

x1% = Rand(0,10000) 
x2% = Rand(0,10000)
y1% = Rand(0,10000)
y2% = Rand(0,10000)

Text 10,10,"Waiting..."
Flip
Cls
Delay SECS_TO_DELAY*1000

Text 10,10,"Testing..."
Flip
StartTime = MilliSecs()
While Not (Calculations = CALCS_TO_PERFORM)
	Distance = Sqr((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2))
	Calculations=Calculations+1
Wend
EndTime=MilliSecs()

Cls
Text 10,10,"Time taken: "+ (EndTime -  StartTime)
Flip
WaitKey


Max Version:
SuperStrict
SeedRnd(MilliSecs())
Const SECS_TO_DELAY:Int = 10
Const CALCS_TO_PERFORM:Int = 100000000

Graphics 800,600,0

Local StartTime:Int 
Local EndTime:Int
Local Calculations:Int
Local Distance:Int

Local x1:Int = Rand(0,10000) 
Local x2:Int = Rand(0,10000)
Local y1:Int = Rand(0,10000)
Local y2:Int = Rand(0,10000)

DrawText "Waiting...",10,10
Flip
Cls
Delay SECS_TO_DELAY*1000

DrawText "Testing...",10,10
Flip
StartTime = MilliSecs()
While Not (Calculations = CALCS_TO_PERFORM)
	Distance = Sqr((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2))
	Calculations:+1
Wend
EndTime=MilliSecs()

Cls
DrawText "Time taken: "+ (EndTime -  StartTime),10,10
Flip
WaitKey


So that's one hundred million distance calculations with the number of milliseconds it took to perform them recorded each time. Here are the results I get:

B3D
===
5114
5210
5245
5211
5062

Max
===
15924
15123
15053
15056
15100

So unless I've made a schoolboy error Max is, as you might expect of an OOP language, actually slower than its procedural counterpart. Interestingly, if I add a minimal framework to the Max code (Framework BRL.GLMax2D, Import BRL.Random) I start getting results in the region of 13000 millisecs rather than 15000.