Microsoft Office 2000/Visual Basic Programmer's
Guide |
|
Loops
The following points provide suggestions for ways to save resources when you are executing loops:
-
Analyze your loops to see whether you are needlessly repeating memory-intensive operations. For example, are there any variables that you can set outside the loop, rather than within it? Are you performing a conversion procedure each time through the loop that could be done outside the loop?
-
Consider whether you need to loop only until a certain condition is met. If so, you may be able to exit the loop early. For example, suppose you are performing data validation on a string that should not contain numeric characters. If you have a loop that checks each character in a string to determine whether the string contains any numeric characters, you can exit the loop as soon as you find the first numeric character.
-
If you need to refer to an element of an array within a loop, create a temporary variable that stores the element's value rather than referring to it within the array. Retrieving values from an array is slower than reading a variable of the same type.