Tip on how not to write speedy code

kolbe

Active Member
Licensed User
I wrote a little program the other day that scanned through a multi-megabyte file looking for certain bit sequences and then decoding them. I was surprised how slow it ran. After some testing I ran across some interesting observations.

Take this simple code below.

B4X:
Sub App_Start
   form1.Show
   form1.Refresh
   For i = 1 To 100000
      label1.Text=i
'      label1.Refresh
      form1.Refresh
   Next
   label1.Text="done"
   form1.Refresh
End Sub
The code above takes about 5 minutes to run. :sign0161:
Change the form1.refresh to label1.refresh and it runs in 10 seconds!
Comment out label1.refresh and it runs in 6 seconds!!
Comment out label1.text and it runs in less than second!!!!

So the point here is that from now on I'll make sure my number crunching, loops, etc.. are done without refreshing the data on screen if I want speedy code. We are talking about 2 orders of magnitude in difference here!!

Going to go back and rewrite some of my code now.

This is was all done on the desktop. I haven't tried this on a PPC yet.
 

agraham

Expert
Licensed User
You might try the DoEvents statement rather than Refresh a specific control. DoEvents processes any message in the apps' message queue and so will update anything that needs updating rather just a specific control. Forcing a full Form Refresh will redraw the entire form, and as you have found out is not a good idea!
 
Top