The smallwig theory of optimization

There are three kinds of optimization.

   1. Optimization by using a more sensible overall approach.
   2. Optimization by making the code less weird.
   3. Optimization by making the code more weird.

You've probably heard, and maybe even spouted yourself, the phrase "premature optimization is the root of all evil." It's exclusively "Type 3 optimization" that this aphorism applies to. Types 1 and 2 are quite fine to engage in pre-emptively.

To make a type 3 optimization, your burdens are six:

   1. Thou shalt have excellent, comprehensive unit tests.
   2. Thou shalt have a reliable benchmark, based on representative inputs.
   3. Thou shalt demonstrate that your change improves the benchmark.
   4. Thou shalt successfully argue that this improvement really matters.
   5. Thou shalt comment the code.
   6. In nontrivial cases, thou shalt also preserve the clear-but-slow implementation, to use in parity tests with your optimized implementation.

In all things, remember these truths:

   1. Your brain is a terrible profiler.
   2. Hotspot will outsmart you.
   3. It just doesn't matter, until it matters.

If you believe this post, please spread the word!

你可能感兴趣的:(optimization)