X
Business

Optimise Visual Basic compiler

The secret in making Visual Basic programs run faster lies in the advanced compiler settings.
Written by Peter Aitken, Contributor
Most programmers compile their VB programs using the default advanced compiler settings. If you know about these settings, you may be able to change them to improve the performance of your compiled application.
You can find VB's compiler settings on the Compile tab of the Project Properties dialog box, which you can display by selecting ProjectName Properties from the Project menu. Click on the Advanced Optimizations button to set these compiler options, as described below.

Assume No Aliasing
An alias is when a program refers to the same variable (i.e., the same memory location) by two different names. It can occur when you use global variables and pass procedure arguments ByRef. Here's an example:

Dim var1 As Long
MySub var1
Sub MySub(var2 As Long)
  var2 = 1     ' Refers to the memory location of var1
               ' because var1 was passed to the
               ' procedure ByRef (the default).
  var1 = 2     ' Refers directly to the memory
               ' location of var1.
End Sub

If your program doesn't use aliases, select the Assume No Aliasing option to permit the compiler to apply optimisations that it couldn't use if aliases were present, thus speeding program execution.

Remove Array Bounds Checks
When your code accesses an array element, VB normally checks to make sure the array element exists; if it doesn't, it triggers a run-time error. For example:

Dim MyArray(100) As Integer
MyArray(200) = 25 ' Error - element 200 does not exist.

If you're confident this error will not occur--for example, if your code uses the UBound and LBound functions to ensure that only legal array elements are accessed--you can select the Remove Array Bounds Checks option for faster, smaller compiled code. (Be careful: If this option is selected and your code writes to a nonexistent array element, it can result in erratic program behavior or a crash.)

Remove Integer Overflow Checks
By default, VB checks the results of every calculation involving integer data types (Byte, Integer, and Long) to ensure that the result doesn't exceed the range of the data type. Selecting the Remove Integer Overflow Checks option disables these checks and speeds integer calculations. (Beware: If an overflow does occur, the calculation will produce incorrect results without a warning or run-time error.)

Remove Floating Point Error Checks
Floating point error checks do the same thing for calculations involving the floating point data types (Single and Double). Select the Remove Floating Point Error Checks option to disable these checks with the same benefits and potential problems as described above for integer overflow checks.

Allow Unrounded Floating Point Operations
When you select the Allow Unrounded Floating Point Operations option, VB can perform certain operations with floating point data more efficiently. The downside is that when you compare floating point values, they may be considered unequal when, in fact, you expect them to be equal.

Remove Safe Pentium FDIV Checks
Some older Pentium CPUs have a bug that produces slightly inaccurate results when performing floating point divisions. VB's default is to check for and correct this inaccuracy.

By selecting the Remove Safe Pentium FDIV Checks option, you disable the check, and your code will perform floating point divisions faster. These old CPUs are pretty scarce these days, so this option is usually a safe one to select.

Author's note: You should apply the advanced compiler options described in this tip carefully, and only if you understand the potential effects on your program.

Peter Aitken has been programming with Visual Basic since Version 1.0. He has written numerous books and magazine articles on Visual Basic and other computer and programming topics.

Editorial standards