Performance

Sergey Kravchenko

Active Member
Licensed User
Longtime User
In his program to change one line - added "Number":
Dim v (4,6), x, y as Number
Productivity remained at the same level.
What should also change the code? :confused:
 

Attachments

  • Fractal.sbp
    973 bytes · Views: 270
  • Fractal.zip
    12.8 KB · Views: 226
  • Fractal680.zip
    12.7 KB · Views: 249

Erel

B4X founder
Staff member
Licensed User
Longtime User
You should also declare the local variables as Numbers:
B4X:
Sub Globals 'Fractal
    Dim v(4,6) As  Number,x As Number,y As Number
    x=0
    y=0
    v()=Array((0,0,0,0.16,0,0),(0.85,0.04,-0.04,0.85,0,1.6),(0.2,-0.26,0.23,0.22,0,1.6),(-0.15,0.28,0.26,0.24,0,0.44))
End Sub

Sub App_Start
    Fractal.Show
    t=Now
    Fractal.DrawString("Fractal",12,10,10,10,30,cGreen)
    Dim i As Number, r As Number, x1 As Number, y1 As Number, c As Number
    For i=1 To 10000 'quality: 10000-70000
    r=Rnd(0,100)/100
    If r>=0 AND r<0.01 Then c=0
    If r>=0.01 AND r<0.8 Then c=1
    If r>=0.8 AND r<0.9 Then c=2
    If r>=0.9 AND r<=1 Then c=3
    x1=(v(c,0)*x)+(v(c,1)*y)+v(c,4)
    y1=(v(c,2)*x)+(v(c,3)*y)+v(c,5)
    x=x1
    y=y1
    Fractal.Line(x*23+100,235-y*23,x*23+100+1,235-y*23+1,cGreen)
    If i Mod 100 = 0 Then DoEvents
    Next
    Msgbox((Now-t) / cTicksPerSecond)
End Sub
The main overhead in this program is caused by refreshing the screen every iteration.
Run the code I posted and you will see that it is running very fast because of this line:
B4X:
If i Mod 100 = 0 Then DoEvents

I see an improvement of 50% between the typed version and the untyped version.
 

klaus

Expert
Licensed User
Longtime User
I made performance comparisons with my, desktop only, 'Dynamic simulations' program with lots of calculation.
The calculation time of a simulation with B4PPC version 6.851, optimzed compiled, is 3 times faster than the 6.80 version.
But the same program in VB is still 5 times faster than the 6.851 version ?

Best regards.
 

klaus

Expert
Licensed User
Longtime User
The program is in VB6.

Attached is the whole project, not a small one, as it is now.
There are lots of calculations but with internal loops and If Then tests.
The time is measured from the click on the calulation button and the end of the calculation and is displayed in Calculation time.

I will have a look at all the variable declarations and set all numbers to Number, after the answer here: http://www.b4x.com/forum/beta-versions/5501-what-variable-type-declare.html#post32390

Best regards.
 

agraham

Expert
Licensed User
Longtime User
There still seem to be variables and structs/arrays not declared as Number so I don't think that you have got the full speed up possible. Note that although declaring arrays as Double in previous versions speeded the calculations up declaring them Number or Double makes a bigger difference in this new version for technical reasons to do with what a typed array actually is now compared to what it was before.

Normally I don't like multi-statement lines but while I've been playing with a pre-beta version I found it useful to to implement typed initialised globals like this to keep the declaration and initialisation together and make the code easier to read.

Dim Something As Number : Something = 1.2345 ' whatever Something is
 

klaus

Expert
Licensed User
Longtime User
I have updated all variable declarations, also the variables for array indexes.
Now the speed increase is a little bit higher than 6.
The VB6 program is now 'only' 2 times faster.
Really a big speed improvement.

A certain number of the array variables were already declared as Double in the 6.80 version.
I removed the Double declarations in the 'old' version, result the 6,851 version is 10 times faster.

Best regards.
 
Top