Optimizing VB Code

Code optimization depends on the type of applipttion you’re writing. In l!\06t cases, .you’ll be optimizing’ small, tight sections of code that are executed freqhently (such as loops or frequently called procedure=- Code optimization requires a combination of experience, an eye for detail, and a understanding of the architecture. of the language and how processors work. Seasoned programmers who have done some assembly language programming in their days have an advantage, but you can pick up a lot along the way.

Youshould never consider your code to have been optimized completely, and never underestimate your capabilities. Some expert programmers start optimizing with the code produced by the compiler, 50 don’t count on the machine too much. Learn its limitations and work around them. Although there-are no hard and fast rules for optimizing code, there are some tricks you can use and some guidelines you can lollow. We’ll discuss them in the following sections, here we’ll see how these tricks affect the native code and code compiler.

Use Proper Data Types

The simplest optimization trick is to use the proper data typesfor your calculations. If your application manipulates integers, don’t use doubles, or even worse, variants. They will slow down the calculations without increasing numerical accuracy. In general, the shorter the data type.the more efficiently it’s handled. Many programmers use floating-point numbers even when they are manipulating integers. Some extra accuracy can’t hurt, so why sf-are,it? The extra accuracy comes at a price, and when it’s not really called for, it should be avoided .

If  you’re not accustomed to declaring variables, you may end ,up using variants for mundane tasks such as loop counters. Even if you don’t use the Explicit option, which forces you to declare every variable before using it~at a minimum, you should declare integer variables.

If you’re going to use the loop counters (variables iCOlJlifct- and jCoullter) in the loop’s body, you may be tempted todeclare them as Doubles to match the accuracy of the remaining variables in the loop. H the loop counters are declared as Doubles, the nested loop will take more than twice as lcng to complete than if the same variables were declared as Integers. Sometimes the loop’s cOWlty exceed the capacity of the Integer data type. A loop that must repeat 50,000~es can’t be set up with an integer counter. In uus case, you should declare the loop counter as long.

When working with Integer data types, you should take into consideration both are nature of the data and the results, For example, consider the difference between two date values that are within the same month:

The DDiff variable holds a Double value, as we discussed in Chapter 3. However, the integer part of the difference is the. number of days between. the two dates. To express this difference in seconds, you must multiply it by 24″60″60. The following statement will fail:

The reason is that Visual Basic will See three integers and assume that the result of .the operation 24~6Q~60is also an integer ..Even if the variable DDiffis declared-as Double (as it should be), Visual Basic will fail to calculate the expression 24*60*60 because the result doesn’t fit into aninteger. To work around this problem, convert the integers to floating-point numbers as follows

This iittle trick will force Visual Basic to generate a floating-point never “[whose decimal part will be zero) and the calculations. will complete.

Scroll to Top