Measure twice, optimize once

What follows are selected quotes from the book C++ Coding Standards: 101 Rules, Guidelines, and Best Practices, authored by Herb Sutter et al, a member on the C++ language committee. The quotes are lightly edited, re-ordered, and sprinkled with my own comments. This particular guideline is not specific to C++ but universal to all programming.


Measure twice, optimize once

Premature optimization is as addictive as it is unproductive. A common beginner’s mistake is to write new code while obsessing – with pride! – over optimal execution at the cost of understandability and maintainability. More often than not, this yields miles of spaghetti that, even if correct in the beginning, is hard to read and hard to change.

We define premature optimization as making designs or code more complex, and so less readable, in the name of performance when the effort is not justified by a proven performance need (such as actual measurement and comparison against goals), and thus by definition adds no proven value to your program. All too often, unneeded and unmeasured optimization efforts don’t even make the program any faster. We programmers are notoriously bad at estimating what code will be faster or smaller, or where the bottlenecks will be. This includes the smartest in our industry, and it includes you.

In the world of front-end web development, this mistake often manifests as an obsession over page weight. We dump library X or framework Y, and instead we write our own home-brew code. We justify it as page weight reduction and assume it will result in huge performance gains. But we rarely measure to confirm, and our assumptions are frequently faulty.

It wasn’t until 2007 that we as an industry even realized the front-end is where most of the time is spent. Before then, even the smartest in our industry assumed back-end applications and databases were the bottleneck. Measurement proved those assumptions wrong. The result of those measurements were published at Yahoo. Of the 35 rules currently listed (which are listed in their order of importance) only three are related to page weight. Of those three, the most important, coming in at #4, is gziping. The next most important, coming in at #10, is minifying. That means there are at least eight other optimization tricks that are more important than slimming your page.

Optimization must be preceded by measurement, and measurement must be preceded by optimization goals. An optimization goal might be, “5Mb connections should get a usable page within X seconds.” Start with the most significant optimizations (remember, Yahoo’s rules are ordered by importance). But above all else, measure. Did your latest optimization have a significant impact toward your goal? If not, don’t do it.

Correctness, simplicity, and clarity come first. It is far, far easier to make a correct program fast than it is to make a fast program correct. So, by default, don’t focus on making code fast; focus first on making code as clear and readable and maintainable as possible.

3 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.