To help you better understand how to apply the rules that have been mentioned so far andoptimize your applications, let’s explore a few practical situations and follow the optimization process. In the rest of the chapter we’ll optimize two of the applications developed earlier in the book: the OirMap application we developed in Chapter 11 and the Image application.
Before you can optimize your application’s execution speed, you must find out which sections of the code call for optimization. Optimizing the statements that display the results isn’t going to help an application if it spends several minutes performing the calculations. Most of the code in an application is rarely executed, and in many cases, not at all. Some statements are executed thousands or millions of times-This is the code that must be optimized. You must locate the sections of the’ code that are responsible for most of the execution time and focus your efforts there.
Sometimes, it’s not easy to locate the statements responsible for an application’s execution speed, especially in large projects developed by programming teams. When you reach-that point, you should consider some professional tools (called codeprofils), which analyze your code and pinpoint the sections of the code where you should focus your efforts
Timing Your Applications
In the course of application optimization, you’ll be comparing the-optimized applications to the original, non-optimized versions of the same applications. Since the same application will be executed on all types of different systems (some of them on l00MHz systems, others on 300MHz systems), the absolute numbers are meaningless. You should always look at the improvement ratio or how much faster the optimized application runs. These numbers shouldn’t change on different processors. The code in this chapter is tested on a Pentium 166MHz computer.
Another issue you should keep in mind is how much memory is installed on the target computer. Even if your computer has plenty of memory for a specific application, you should also test the application on a system with less memory and include a warning for the users, should the program slow down considerably if executed on a computer with the minimum amount of memory. This isn’t going to affect the execution speed per se (in other words, you can’t make your application run faster by adding memory), but too little memory can cause other side effects. . If there’s not enough memory on your computer and the operating system can’t keep all open Forms in the memory, it will have to use the swap file on the computer’s hard disk. The swapfile isa large-area on the disk that the operating system uses as memory when it runs out of physical RAM.
The swap file practically eliminates the “out o.fmemory” problems, but the hard disk isn’t nearly as fast as the RAM, This will introduce unacceptable delays that aren’t caused by your code. Therefore, when you test an application, you should keep an eye on the usage of the hard disk. H the disk is spinnirig when the code isn’t accessing files, chances are you don’t have enough memory installed and the same application will execute much faster on a system with more RAM. The system I used “fortesting the code in this chapter is equipped with 64MB of RAM.
To time an operation, you must use the Timeri) function, which returns the number of seconds (and fractions thereof) that have elapsed since midnight. However, it has a resolution of 55 milliseconds, so to time operations that take less than half a second or so, you should use the API time functions (discussed in Chapter 13). For the purposes of this chapter, the Timert) function is more than adequate. Save the function’s value to a local variable before the first executable statement of the section you want to time. At the end of this section, call the Tuner{) function again and subtract the two values. Here’s a typical code timing section:
{other statements}
T1 = Timer()
{code you want to time}
TDiff – Timer() – T1
Debug.Print TDiff .
You can also place the operations you want to time in a loop that’s repeated manytimes, but you should be very careful. If the time it takes Visual Ba sic to set up the loop and update the loop’s counter isn’t negligible compared to the time it takes to complete the calculations, you’ll be timing the loop as well.