How to optimize this code?

tinmanjo

Member
Licensed User
Im trying to create a bouncing ball demo to test performance graphically (without physics) using only the forms paint method but adding more than 4 balls makes it incredibly slow on the ppc, can anyone suggest an optimal way to loop through the ball properties, I know that using the sprite library might help but I wanted to get the best performance first out of just raw code and the form methods.


Here it is:-

B4X:
'Program: Program to demonstrated the speed of the fill rountine for forms
'         several bouncing balls with no physics.

Sub Globals
   'Declare the global variables here.
   QUIT =False
   ' balls coordinates are in array
   ' hence Ball(n,(xpos,ypos,xspeed,yspeed),)  n= number of balls
   NumBalls=3
   Dim Balls(NumBalls,4)
   
   'Setup balls
   For i=0 To ArrayLen(balls(),1) -1
   
   balls(i,0)=Rnd(100,200) 'xpos
   balls(i,1)=Rnd(100,250) 'ypos
   balls(i,2)=Rnd(-10,6)   'xspeed
   balls(i,3)=Rnd(-10,7)   'yspeed
   
   Next i
End Sub

Sub App_Start
   Canvas.Show
   timer1.Interval=20
   timer1.Enabled=True
   
   
   'Msgbox("End")
   
End Sub





Sub Timer1_Tick
   
   For i=0 To ArrayLen(balls(),1) -1
      canvas.Circle(balls(i,0),balls(i,1),10,0,0,0,f)
      balls(i,0)=balls(i,0)+balls(i,2)
      balls(i,1)=balls(i,1)+balls(i,3)
            
      If balls(i,0)>(220-5) Then 
         balls(i,2)=-balls(i,2)
         balls(i,1)=balls(i,1)+Rnd(0,3)
      End If
      
      'If balls(i,2)=balls(i,3) Then 
      'balls(i,2)=Rnd(balls(i,2),3) 'randomize if 'speeds equal out
      'balls(i,3)=Rnd(balls(i,3),3)
      'End If
      
      If balls(i,1)>(250-5) Then 
         balls(i,3)=-balls(i,3) 
         balls(i,0)=balls(i,0)+Rnd(0,3)
      End If
      
      If balls(i,0) <10 Then balls(i,2)=-balls(i,2)
      If balls(i,1) <10 Then balls(i,3)=-balls(i,3)
      If balls(i,1)=250-5 Then balls(i,1)=balls(i,1)-1
      If balls(i,1)=10 Then balls(i,1)=balls(i,1)+1
      
      
      If balls(i,0)>260 OR balls(i,0)<-50 Then balls(i,2)=0  'kill ball if it ventures off
      If balls(i,1)>400 OR balls(i,1)<-100 Then balls(i,3)=0
   
   canvas.Circle(balls(i,0),balls(i,1),10,Rnd(10,255),Rnd(10,255),Rnd(10,255),f)
Next i
End Sub
 

mjcoon

Well-Known Member
Licensed User
I think that the code would be much easier to follow if you used a structure for each ball. Really that just enables you to name the four elements as you have done in the set-up with comments.

But this would not impact the performance, I think. One thing to try would be to extract the four elements from the array into local variables at the top of the loop and return them to the array at the bottom. This would avoid the large number of array references which must each involve some underlying calculation to re-establish the current ball. Incidently the local variables could have names like xpos and ypos, going some way to circumvent the comment in my first paragraph.

I'll leave advice on the graphics aspects to those that understand such things...

Mike.
 

tinmanjo

Member
Licensed User
Thanks for your suggestion, il look into it.

I did think of transfering the ball's properties into variables before the loop

B4X:
for i=

xpos=balls(i,0)
ypos=balls(i,1)....

next i


sorry if my code is noobish, im still learning.
 
Last edited:

nick2210

Member
30 balls

Hello.
I have attached my version of your programm.I think you may add as many balls in there as you like.
Well...I must compliment you on good knowledge of 2 dimential arrays.
Still do not understand what the last four "if"s in Sub timer1_tick for.
Regards
 

Attachments

  • second.sbp
    1.2 KB · Views: 191

tinmanjo

Member
Licensed User
Thanks for your contribution, Im puzzled as to how defining a type object being set is faster than an array. I guess the performance on arrays is not as good, i need to check this on ppc to see the difference.

Thanks for the tip.
 
Last edited:

tinmanjo

Member
Licensed User
The Second.sbp Setting type doesnt improve performance on pocket pc (kaiser), I guess il have to use ImageLib instead.
:(
 
Top