Introduction

"More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity." - W.A. Wulf "On the other hand, we cannot ignore efficiency." - Jon Bentley

Many software engineers recommend what I call the "procrastination approach" to optimization. Delay optimization as much as possible, and don't do it if you can avoid it. I agree with the basic premise. Optimizing too early or too often is not a good approach to engineering. Better to have a program that runs than a fast program that crashes. On the other hand, you're not likely to write a successful app these days without doing optimization at some point in the process. Your compiler can help you, but you as a programmer understand more about your application than the compiler. As Michael Abrash puts it, the best compiler is "between your ears."

There are many levels of optimization, but I'm going to focus on one in particular: C++ optimizations. Some of these techniques apply to other languages as well - like Java - but most are specific to C++. I'll also cover how to configure your compiler for maximum C++ efficiency.

Preliminaries

I originally wrote this document in 1998. A lot has changed since then, though many of the techniques listed are still relevant and valuable depending on your platform and compiler. As always, never accept optimization techniques at face value. Measure, measure, measure.

All of the examples are in C++. The code is designed to compile with any standard ANSI C++ - compliant compiler. Some of the more complex techniques involve templates and the Standard Template Library. I used