Compiler Options

So, now that we have a native code compiler, which produces faster-running executables, you might think that we could just forget all about p-code. Rut the native code compiler comes with a price. It produces longer files that take longer to load, and if you’re distributing your software on the Internet, it will also take longer to download. If these restrictions don’t apply to you, you can compile your applications to native code, see what difference the compiler makes, and then decide how to dlstance,bute them. AppliCations complecation to native code don’t always run faster. Real-world-bustness applications spend most of their time doing disk I/O (input/output), screen updates, and other tasks-that you can’ . speed up from within your Visual Basiccode.
For example, when you call an API function, you’re no longer in control. While the API function is executed, there’s nothing you.can do t~ ~mprove its speed {to mat,ter how much you’ve optimized the rest of the code.In a way, this is a barrier you can’t break regardless of whether you compile to p-code or native code. In the section “Qptimizing the DirMap Application,” at the end of this chapter, you’ll see that the code that manipulates the properties of Active controls can’t be made to go’ any. faster.

With these issuesin mind, let’s go through ‘the process of compiling applications and look at the various options of the native code compiler. To create the executable file, start Visual Basic (if necessary). and chooseFile “. Make file.exe (file.exeis the application’s name). Before you create the EXE file, however, you mustspecify the compiler’s options. Open the Project menu and select the last ‘ command, Project Properties,. to see the Pro.ject Properties dialog box.

The type of executable that will be created depends on the settings you choose in the Compile tab of the Project Properties dialog box shown in figure 12.1. Compile to P-Code.compiJes a project using p-code. The main benefit of p-code is that it’s compact and (in many practical cases) not much slower  than purely executable code the Compile to .Nati~eCode compiles a project using native code, which is ~he;1 .machine language the CPU can !:Jnderstand and cxecute.

Options for ‘Optimizing’Native Code The native code compiler must balance a number of requirements, some, of which conflict. For example, it can’t produce the most compact and fastest possible code, The Compile to Native Code  section of the Project Properties dialog box contains processor-specific optimizations and anurnber of advanced options that mayor may not apply to your project. You should specify the options that will produce- the type of executable best matches your application and the target systems.

• Optimize for Fast Code maximizes the speed’ of the executable file by instructing the compiler to favor speedoversize.To optimize the code, the compiler reduces many constructs to functionally similar sequences of machine code. This will inevitably increase.the size of the executable file. Optimize for Small Code minimizes the.size ofthe executable file by instructing the compiler to favor size over speed. No Optimization compilc:.swithout specific optimizations ..!t balances  speed and size. Favor Pentium Pr~TMoPtimizes the code to favor th~ Pentium Pro processor. Use this option for programs meant only for the Pentium Pro; Code generated with this option will runon other Intel processors, but it rnav not perrmas well. ‘

• Create Symbolic Debug Info generates symbolic debug information in the executable. An executable file created using this option can.be debugged using Visual C++ or debuggers that use the CodeView style of debug information, Setting this option generates a PDB file with thesymbol information for your executable. Thisoption is most often used byVisual C++ programmers who’ ‘ also use Visual Basic.

• ‘:Advanced Optimizationsdisplays the’Advanced Optimizations dialog box,which is shown in Figure 12.2. Use the options in this dialog box  to turn off , ‘certain checks that normally take place to ensure that your application works properly, To increase the speed of the executable file, you can turn off some or all these options by-checking the appropriate checkbox

Assume.No.Aliasing tells the compiler that your program doesn’t use aliasing. Aliasing is a technique that lets your code refer to th~ same variable .” (memory location) by.more than one name. We don’t use this technique (of dubious value, anyway~ in this book, so you can safely turn off this option.
Remove Armay Bound-Checks tells the program not·to check for array bounds. By default, Visual Basic checks an array’s bounds every’ time your code accesses the array to determine if the index is within the range of the array. If the index is not within the array’s bounds, a runtime error is be dated (the “Out-of-bounds” error), which can be trapped from within the However, the code that ensures that the’ array’s bounds aren’t exceeded is costly iil terms of execution time. If an array bound is exceeded, the results: will be unexpected. You can turn on this option if the application doesn’t use arrays or if you know that your code will not attempt to access non-existent array.elements. For example, a loop, that uses the LBoimdO and UBound() functions to scan the elements of an array won’t cross the array’s bcundaries’
Remove Integer Overflow Checks speeds up in~eger calculations when Checked. By default, .Visual Basic checks every calculation for integer-style data types Byte, Integer, and Long-to ensure that the result is within the range of the data type. If the magnitude of the value being put into the data type-is incorrect, a.runtime error is generated. If this option is checked and ·data type capacities overflow, you’ll get incorrect results, but no errors. It’s :really difficultto make sure that calculations with integers do not result in overflows, and it’s even trickier to find out where the overflow occurred.

Remove Floating Point Errot Checks speeds up floating-point calculate a floating-point data type-Single and Double—to make sure that the result is within range for that data type and that there are no divide by zero or invalid operations, If the magnitude of the value being put into the data type is incorrect, an error occurs. Select this option to turn off the error checking and speed up floating-point calculations. If this option ischecked and data type capacities are overflowed, no error will occur, and you may get incorrect results.
• Allow Unrounded Floating Pomt Operations.uses floating-point registers more effioently, avoids storing and loading large volumes of data to and from memory. and performs floating point comparisons mo~ efficiently (floatingpoint numbers are slightly truncated when moved from registers to memory) ..
• Remove Safe Pentium TM FDIV Checks removes Safety checking so that the code for floating-point division is faster. Selecting this option may produce slightly incorrect results on Pentium processors with the FDIV bug. (The new Pentiums that are 120MHz or faster are exempt from the floating-point divisionflaw.)

Use the native code compiler with these advanced options to automatically speed up your application. Of course, how much faster the application will become depends in the nature of the application, but the compiler will do its best to ¢oduce the fastest code possible. Unless-the EXEis really 1aJge(and you plan to distribute it by means other than a CD-ROM), you should generate EXE files optimized for speed. However, a few bad coding techniques can easily offset any benefit introduced by the native code compiler. For example, if your application calculates squares in a loop that’s executed many times and you code it as x.•2•., no advanced optimization option is going to help your application. As you’ll see shortly, by coding this operation as x*x.it will make a p-code executable’run faster than a native code executable that uses the expressionx .•2•. The’trick to writing fast-running applications is to start by optimizing the code itself. In the following sections, we’re going to present a few useful optimization techniques. They are quite simple, but very efficient. Even if you’re an experienced VISUal Basic programmer, you should take a look at the following pages. You may discover some new optimization tricks.

Scroll to Top