Bmax

BlitzMax Forums/BlitzMax Programming/Bmax

Najdorf(Posted 2005) [#1]
(woops title should be "Bmax optimisation problems")


It seems to me that bmax, while very fast at drawing, has some optimisation problems:

This:

Function sq(x#)
Return x*x
End Function

t=MilliSecs()
Local a#

For i=0 To 10000000
a=sq(2)
Next

Print (MilliSecs()-t)


runs in 700 millisecs

While this:

t=MilliSecs()
Local a#
For i=0 To 10000000
a=2*2
Next
Print (MilliSecs()-t)


runs in 70 millisecs.

'Cmon, it 10 times more!! This discourages the use of functions a lot!

I dont know what C does, but I would be surprised if it took 10 times more.

The second example


Function f(x#)
Local a#=x+3
Local b#=a*5
Return b

End Function


t=MilliSecs()

Local a#
For i=0 To 10000000
a=f(2)
Next

Print (MilliSecs()-t)



takes 950 millisecs

while



Function f(x#)
Return (x+3)*5

End Function

t=MilliSecs()

Local a#
For i=0 To 10000000
a=f(2)
Next

Print (MilliSecs()-t)



takes 700 millisecs, while they should be the same if it was decently optimized


AL(Posted 2005) [#2]
I think the compiler is optimizing the a=2*2 part (maybe just converting it to a=4?).

I changed the function to the following and its run time is much closer to the non-function method:

Function sq(x#)
Return 2*2
End Function

t=MilliSecs()
Local a#

For i=0 To 10000000
a=sq(2)
Next

Print (MilliSecs()-t)



Najdorf(Posted 2005) [#3]
Hmmmm... perhaps...

I'm courious about

t=MilliSecs()
Local a#
For i=0 To 10000000
a=i*i
Next
Print (MilliSecs()-t)


and the functional version

Function sq(x#)
Return x*x
End Function

t=MilliSecs()
Local a#

For i=0 To 10000000
a=sq(i)
Next

Print (MilliSecs()-t)


in this case you couldn't use that trick though


FlameDuck(Posted 2005) [#4]
I think the compiler is optimizing the a=2*2 part (maybe just converting it to a=4?)
You are correct. Although why such optimization is a problem is beyond me.


Floyd(Posted 2005) [#5]
The sq() test is probably spending all its time converting between data types.

Function sq(x#)
    Return x*x
End Function

t=MilliSecs()
Local a#

For i=0 To 10000000
    a=sq(i)
Next

a=sq(i) must convert the integer i to a float, convert the float result x*x to an integer return value and finally convert that to a float for assignment to variable a.


Robert Cummings(Posted 2005) [#6]
This would also be a prime candidate for inlining. The compiler would do well to support that kind of thing.

Microsoft's compiler automatically inlines where it can, and you can specify inlines if you must. Don't think you'd save too much though it is far more readible code-wise.