In this chapter, I will present the basics of code optimization and a few tricks for writing faster running code. To demonstrate the importance of code optimization, I will also present a few examples based on applications developed earlier in the book. Visual Basic is the simplest, most popular language for rapid application development, and in most respects, as powerful as any other language for Windows application development, such as Visual C++ or Delphi. But Visual Basic has limitations. For one, you shouldn’t expect that an environment with the simplicity of Visual Basic will be the most efficient or speedy. Yet,it’s very efficient, and after the release of version 5 (the first version of the language with native code compatibility), it’s closing the gap in execution speed when compared to languages like Visual C++. Visual Bfsic.comes with a native code compiler, which can generate optimized code similar to the code produced by the VC++ compiler. In this chapter, we’re going to discuss the Visual Basic compiler, look at what the native code compiler can do for your applications, and give you a few tips on how to optimize your code for maximum execution speed. Of course, the speed of an application isn’t determined exclusively by the compiler. If the source code is sloppy, the executable’s speed won’t be optimal.
An application can be optimized in many ways. Some programmers will spend : most of their time tweaking the user interface. Sometimes, a funky interface can make an application really fun to use. Most programmers will tweak the code to optimize speed. They may also consider tweaking the size of the application (the total size of the installation files, since you can no longer distribute stand-alone EXE files). As far as the appearance goes, it’s a question of artistic talent more than anything else. And since CD-ROMs are the most common distribution medium, there’s not much point in optimizing the size of the application. Therefore, this chapter will deal exclusively with execution speed. When you optimize an application, you should also try to optimize your own time. Decide which parts of the application will benefit from optimization and focus on them. Try to optimize areas where a little work will make a lot of difference. For example, if you’re developing an image processing application, you should focus on the code that processes the image. Subroutines that aren’t going to be called often, such as a procedure that rotates the image, aren’t going to disturb the user as much. But if the basic procedures of the application (the ones that are called again and again in the course of the application) are slow, users will most likely consider an equivalent, but faster, application.You should also know when to stop. Optimization is a challenging process and you may find yourself in a situation where you’re competing wijh yourself or trying
to prove something to yourself by seeking further improvements in speed. This is great motivation while learning, but when you develop as a professional, you should know when to stop. It doesn’t make much sense to delay an entire project in order to speed up by 5% some feature of the application that most users will never need.
The optimization process practically never ends. You can always find places in your code that can be rewritten to make the program run 2% faster. For experienced programmers, almost all applications of considerable size can be further optimized. But what’s the point in wasting a few hours of the development cycle to make a calculation run faster if the entire calculation doesn’t take more than half a second? Or, what’s the point incalculating a section of an application that very few users will invoke (for example, the code that converts a file to a newer format)? Similar “special” sections of an application don’t need to be optimized. I’m not suggesting that you make them terribly slow, but they probably won’t be worth your time or effort to optimize.