Avoid Large Strings VB Help

The single most important trick in speeding up string operations is to remember that MidO is not only a function, it’s also a statement. In the following line, the Midt) function returns the third through the tenth characters of a string:

subString  m.MidS (originalStrig,3,10)

But in the following line, Mid$O is used as’a statement to change ‘part of the string:

MidS (originalString,3,10) = ‘1234567890’

The inner loop creates 3,OOO characterstrings (variable BStrillg) at.a time and appends it to the AString variable. It must build ten such strings to fill the long string. Yet,this structure takes only 6.7 seconds on the 166MHz Pentium. By limiting the size of the string variable used to append individual characters, you cart bring the total execution time from more than 60 seconds down to less than seven ·seconds. Notice that the AString variable is updated only 10 times, not with every new character. That’s where the gain in execution time comes from.

AccessObject Properties Indirectly

Another simple, effective optimization technique is to avoid referencing the same property many times. Let’s-say you have an incoming stream of data that you want to display OJI a TextBox control. You already know what to do: create small string variables and append them to the control’s Text property. Another common mistake is to reference a property like Textl.Text in a loop. Let’s say you want to reverse the characters of the text displayed on a TextBox control.  Even if you use the Mid() statement in a loop such as the following, the process is going to be very slow:

Because a lot of overhead is involved every time you access a control’s property, avoid too many references to the Same property. Copy the Text property’s value to . an intermediate variable and process this variable. Then, assign the processed variable to the Text property. Reversing 3,000 characters on a TextBox control takes 8.6 seconds using se Text property, while the same operation using a temporary string variable takes less than half a second on the Same computer. Clearly, you should avoid referencing control properties directly in your code, especially within loops that are executed many times.
The same is true for all properties, but they don’t usually appear in loops. The Text property can appear in a loop, especially if you append to it characters read from a file or when you save its value to a file. You should always use intermediate string variables and assign to them the value of the Text property

Maximize Subjective Speed

No amount of optimization is going to do you much good unless the users of your application think it’s fast. Conversely, the sloppiest application need not be optimized if users think it’s fast. The most important measure of the speed of an application is its so-called subjective speed (also called perceived speed or apparent speed). Subjective speed can’t be measured in time units (or any units, for that matter). Subjective speed describes how fast the application appears to the user. If your Forms load slowly, no amount of optimization in the code that follows the loading of the Form is going to help. An application that loads, responds, and repaints the screen quickly, may actually be preferred to an application that does its math faster, but , takes too long to load, or one that doesn’t respond quickly to user actions;

Subjective speed can be improved in many ways, but there are a few goals you should always try to attain, they are:

• Try to load the Forms quickly.
• Make sure the application starts quickly.
• Display flash screens, animation, or at least a progress indicator if the program is busy for more than a few seconds.
• Wisely use the AutoRedraw property to speed up graphics .

Your best bet is to load Forms at start-up and keep them in memory. The Load command loads a Form, but doesn’t display it. Once the Form is in memory, you can display it instantly with the Show method. The application may take longer to start, but once it’s started; Forms can be displayed and hidden instantly (unless they contain large bitmaps). But how about the initial delay, when the user has to wait for your application’s Forms to load? This initial delay is just about the worse thing you can do for the application’s subjective speed, but it helps the subjective speed of the rest of the application. Design a simple initial Form that can be loaded very quickly. Once this
• Form is shown, load other larger Forms “”hile the initial Form is displayed. This technique is demonstrated in Chapter 4 in the section “VB6 at Work: The Form-Load Project.”

You should also avoid lengthy (or slow) code segments in a Form’s Load event, U the Load event takes a few seconds to execute, the Form won’t be drawn until the Load event handler has completed its execution. If you must place a significant amount of code in the Form’s Load event, call the Form’s Show method at the beginning. The following statements will force Visual Basic to.draw the Form on
the screen before it starts executing the code:


The DoEvents statement will give Windows a chance to redraw the screen. If you omit this statement, you’ll see ‘only the outline of the Form. Even if the code that follows loads anothe~ Form, you must call the Show method and the DoEvents statement.
When a time-consuming operation takes place, you can use the hourglass to indicate that the user should wait. We’ve all watched more than our fair share of hourglasses. Sometimes users go as far as shake them to see if the ~and will move An hourglass won’t improve the application’s subjective speed’ so we need something more amusing, something fresher. An animated cursor or an AVI file with a few small frames isn’t going to make the application run faster, but it’s not going to slow it down noticeably either. A typical example is the animation that takes place when documents are sent to the Recycle Bin. Another example is the information displayed on the screen while applications are being installed. The flash screens’ keep the user from getting utterly bored, and they are more fun to watch than an hourglass.
Some operations are inherently slow and no amount of optimization will make them fast. If an operation takes more than afew seconds to complete, you should provide an indicator to let the user know the progress of the calculations. An hourglass tells the user that the application is busy doing something, but it gives no cues as to how long it will take to complete: The Progress Bar control is the ideal control for this purpose. The progress indicator lets the user know how the calculations are proceeding and how much longer they will take to complete. You should also provide aCancel button, just in case the user decides to cancel an operationthat takes too long.

Finally, if your application makes extensive use of graphics methods, estimate the effect of the setting of the AutoRedtaw property of the Form or Picture Box where the drawing takes place. The graphics methods are slower when AutoRedraw is True, but the FOI”I!l will be repainted instantly when the user switches to it if it has temporarily hidden by another window. If AutoRedraw is False, then the Form won’t be repainted automatically and you’ll have to redraw the Form from within the Paint event. The effects of the AutoRedraw setting were discussed in detail in the section “Optimization Issues”.

Posted on November 6, 2015 in Optimizing VB Applications

Share the Story

Back to Top
Share This