Wednesday, April 16, 2008

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!

11 comments:

  1. The remedy for premature optimization is to expedite knowledge.

    ReplyDelete
  2. I'd argue that #1 and #2 aren't really optimizations at all. Using a more sensible approach and/or making the code less weird are benefits in and of themselves, irrespective of their effect on performance. They are things you would do even if they made the code slower, and refusing to do them when they make the code slower is a case of premature optimization. Of course if they do make the code faster (and they usually do) then so much the better; and even if they don't make the code faster immediately, these sorts of improvements usually make the code more amenable to carefully planned out, non-premature optimization later.

    ReplyDelete
  3. Also, what about option 2.5? Optimization that neither makes the code less nor more weird, simply different? My gut is that that is premature, if or no other reason that it wastes time that could be spent elsewhere.

    ReplyDelete
  4. Very nice and useful!

    Personally, I am very interesting to find out what is really happening in the real word nowadays. Do the professional programmers of our generation optimize too late or too early? What is most frequent?

    Do you happen to know any surveys
    or polls which can provide objective information regarding the way the programmers use to optimize nowadays?

    Thank you very much,
    Jim Xochellis

    Homepage: http://jimxoch.freehost.gr/

    ReplyDelete
  5. Anonymous11:16 PM

    Hello, everybody. I am a new hand to be here. So nice to meet you all
    !......
    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    ReplyDelete
  6. Computer viruses could be a nightmare. Some can wipe out the knowledge with a challenging push, tie up traffic with a computer community for hrs, convert an innocent machine into a zombie and replicate and mail by themselves to other computer systems. If you've got hardly ever had a machine drop victim to the laptop or computer virus, you could possibly surprise exactly what the fuss is about. Though the issue is easy to understand -- in accordance to Buyer Stories, computer viruses assisted lead to $8.5 billion in client losses in 2008 [source: MarketWatch]. Computer viruses are just just one type of on the internet threat, but they're arguably the best known from the bunch.dell latitude e6510 battery
    Dell inspiron 6400 Battery
    Dell inspiron e1505 Battery F
    Dell latitude d620 Battery
    Dell latitude d610 Battery

    ReplyDelete
  7. I really like your writing style. Nice Post keep it up. Thanks for sharing your information.
    Fashion bloggers in India

    ReplyDelete