Imagine that you are about to finish a relatively large program, one that has taken a few weeks or months to write and debug. Just as you are putting the finishing touches on it, you discover that it is either too slow or runs out of memory when you feed it a realistic set of input data. You sigh, and start the task of optimizing it.
But why optimize? If your program doesn't fit in memory, you can just get more memory; if it is too slow, you can get a faster processor.
I have written Optimizing C++ because I believe that this common attitude is incorrect, and that a knowledge of optimization is essential to a professional programmer. One very important reason is that we often have little control over the hardware on which our programs are to be run. In this situation, the simplistic approach of adding more hardware is not feasible.
Optimizing C++ provides working programmers and those who intend to be working programmers with a practical, real-world approach to program optimization. Many of the optimization techniques presented are derived from my reading of academic journals that are, sadly, little known in the programming community. This book also draws on my nearly 30 years of experience as a programmer in diverse fields of application, during which I have become increasingly concerned about the amount of effort spent in reinventing optimization techniques rather than applying those already developed.
The first question you have to answer is whether your program needs optimization at all. If it does, you have to determine what part of the program is the culprit, and what resource is being overused. Chapter 1 indicates a method of attack on these problems, as well as a real-life example.
All of the examples in this book were compiled with both Microsoft's Visual C++ 5.0 and the DJGPP compiler, written and copyrighted by DJ Delorie. The latter compiler is available here. The source code for the examples is available here. If you want to use DJGPP, I recommend that you also get RHIDE, an integrated development environment for the DJGPP compiler, written and copyrighted by Robert Hoehne, which is available here.
All of the timings and profiling statistics, unless otherwise
noted, were the result of running the corresponding program
compiled
with Visual C++ 5.0 on my Pentium II 233 Megahertz machine with 64
megabytes of memory.
Another topic that is too often overlooked is backing up your computer. You should
always make sure that you have a recent backup, because losing
data is the last thing that you want to have to worry about. But
even with a recent backup, you could still lose some data, so it's
good to know that
(depending on your exact storage setup) there are options like RAID data recovery
that you can use to help get your information back. Taking these
precautions may seem to be unnecessary, but they are a good thing
to do whenever you are making substantial changes to your
computer.
I am always happy to receive correspondence from readers. If you wish to contact me, the best way is to visit my WWW home page.
If you prefer, you can email me.
In the event that you enjoy this book and would like to tell others about it, you might want to write an on-line review on Amazon.com, which you can do here.
I should also tell you how the various typefaces are used in the
book. HelveticaNarrow
is used for program listings,
for
terms used in programs, and for words defined by the C++ language.
Italics
are used primarily for technical terms that are found in the
glossary,
although they are also used for emphasis in some places. The first
time
that I use a particular technical term that you might not know, it
is
in bold face.
Now, on with the show!
A Supermarket Price Lookup System
Cn U Rd Ths (Qkly)? A Data Compression Utility
Free at Last: An Efficient Method of Handling Variable-Length Records
Heavenly Hash: A Dynamic Hashing Algorithm
Zensort: A Sorting Algorithm for Limited Memory
Mozart, No. Would You Believe Gershwin?