Tip on how not to write speedy code

Discussion in 'Code Samples & Tips' started by kolbe, Jan 26, 2008.

  1. kolbe

    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.

    Sub App_Start
    For i = 1 To 100000
    '      label1.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.
  2. agraham

    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!
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice