After you have selected Compile to Native Code, select the first option button
underneath it to generate the fastest code possible (the Optimize for Fast Code
option). Even if the compiler decides that it needs to produce more machine instruction
code to handle certain portions of your application, thereby resulting in a bigger
EXE file, the end result ought to be faster than the smaller alternatives.
You may wonder how one set of instructions can be faster than another, if each
accomplishes the same end result. Well, you can get to your next-door neighbor’s
house by walking about 25,000 miles around the earth or by walking a few steps
the other direction and knocking on the door. You get the same result either way.
A compiler that can optimize for speed just knows how to take those shorter routes.
The VB programmer can perform some kinds of optimizations. VB doesn’t
short-circuit expressions like C or C++, for example. That is, in a conditional
expression such as the following:
If iConditionOne < 1 and iConditionTwo < 10 then
‘ do something
End If
VB evaluates both parts of the conditional expression every time. Even if the
value of iConditionOne were 5, so that the overall
expression must evaluate to False, VB would still evaluate the value of
iConditionTwo. If a C or C++ compiler evaluated this conditional, it would
know that the overall expression must evaluate to False as soon as it evaluated
the first expression. This is called short-circuiting.
If a programmer knows that VB doesn’t short-circuit logical expressions,
it is simple to develop the more efficient habit of coding like this:
If iConditionOne < 1 then
If iConditionTwo < 10 then
‘ do something
End If
End If
It takes two extra lines of code, but the second fragment executes more quickly
than the first when the first condition is false. In this case, knowing how the
language behaves makes it possible for you to write smarter code.
Optimization for performance generally occurs in two ways: globally and at
the register level. If a compiler employs global optimization methods, it tries
to change the order in which your program’s instructions are executed. This
can save time if an action is being repeated unnecessarily, as in a loop such
as this:
Do
iBadlyPlacedVariable = 1
‘ more processing occurs here, but
‘doesn’t change the value of the variable
Loop
In this case, the variable is assigned a value of 1 every time this loop repeats.
If the loop iterates several thousand times, that’s several thousand unnecessary
assignments. Clearly, the assignment should have been done outside the loop, but
the programmer made a mistake. If it uses global methods, an optimizing compiler
can correct this mistake.
Register-level optimization tries to save time by putting data where it can
be reached most quickly. Generally speaking, the data your computer needs can
be found in one of just three places. In order of increasing speed, these are
as follows:
-
Physical storage
-
Random access memory
-
A CPU register
When possible, register optimization tries to put data into a register for
quick access.
It takes a relatively long time to find data on a physical storage device such
as a hard disk. Even fast hard disks have average seek times measuring in the
millisecond range, which is an awfully long time compared to the nanoseconds used
to measure RAM chips. Given a choice, it is always better to search RAM than to
search a hard disk.
(That’s why disk-caching programs are useful: They store recently accessed
data from the hard drive in memory for faster access.)
Because they are part of the CPU itself, registers are even faster than RAM.
If data or an instruction is in RAM, the CPU has to wait for it to be copied into
a register to do anything with it. If it is already in a register, the CPU obviously
doesn’t have to wait on the RAM access operation. Register optimization
occurs when a compiler can reduce the amount of register manipulation necessary
to give the CPU what it needs to run the program.
You will see how well the basic speed optimizations work shortly; but first,
this section looks at the other basic optimization choices. The explanations of
the remaining basic optimizations will be brief;
they’re quite simple.