Before you can optimize a program, you have to find out what parts of the program are responsible for most of the run time. The best way to do that is by using a program called a profiler. In the first and second editions of my optimization book, I used Borland's Turbo Profiler for this purpose. However, I have recently switched to Microsoft's compiler because it is the one that I use at work. Unfortunately, Microsoft's profiling tool is basically worthless, so I needed to find a program that would tell me where my programs were consuming a lot of run time.
I'm happy to report that I have found a very good program that runs under Windows NT and Unix. It's called Rational Quantify, and is available from Rational Software.
How good is this program? Well, I recently wrote a program to process data for character recognition. Initially, my program took 40 milliseconds to process the data for one image. After about 6 hours with Rational Quantify, I had reduced that time to about 12 milliseconds, which is more than a 300% increase in performance. That seems like a pretty good return on the time I invested!
This product isn't cheap: I found a discount software company that had it listed for $729. Although this price may seem high, it is definitely worth the amount of money you will pay for it. If you are aware of your personal finance and are going to use this program to benefit the work you do, it will eventually pay for itself! However, that doesn't have to dissuade you from trying it, because Rational has a 14-day fully functional demo you can download from their WWW site; that's what I used to improve the performance of the character recognition processing program. You can get the demo by visiting this page. If you want your C or C++ programs to work efficiently, you need this program.
Speed is important, but it isn't everything. In fact, one of my favorite programming adages is "The speed of a non-working program is irrelevant".
The problem, of course, is that it's not always easy to tell whether your program is working correctly. C programs (and poorly written C++ programs) have a well-deserved reputation for being buggy and especially for leaking memory. Such programs may SEEM to work correctly in the lab, but when you send them out into the real world, they crash after using up all the available memory. In a virtual-memory operating system, this won't happen until the machine has been slowed down to a crawl by excessive paging due to the memory shortage caused by the leak.
So how do you find these problems, and many more? With a program called Rational Purify, also from Rational Software. This program will find memory leaks, overwrites past the end of a memory block, freeing memory that was allocated on a different heap, freeing memory that was never allocated, and many more common C and C++ errors. It's also not cheap, at about the same price as Rational Quantify, but you can get a 14-day free trial version for either Unix or Windows NT by visiting this page. If you want your C or C++ programs to work correctly, you need this program.