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:

Fatih Coşkun said...

I believe; starred.

Unknown said...

The remedy for premature optimization is to expedite knowledge.

Elliotte Rusty Harold said...

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.

Elliotte Rusty Harold said...

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.

Jim Xochellis said...

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/

Anonymous said...

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

combattery84 said...

Dell Inspiron 600m battery
Dell Inspiron 8100 Battery
Dell Y9943 battery
Dell Inspiron 1521 battery
Dell Inspiron 510m battery
Dell Latitude D500 battery
Dell Latitude D520 battery
Dell GD761 battery
Dell NF343 battery
Dell D5318 battery
Dell G5260 battery
Dell Inspiron 9200 battery
Dell Latitude C500 battery
Dell HD438 Battery
Dell GK479 battery
Dell PC764 battery
Dell KD476 Battery
Dell Inspiron 1150 battery
Dell inspiron 8500 battery
Dell Inspiron 4100 battery
Dell Inspiron 4000 battery
Dell Inspiron 8200 battery
Dell FK890 battery
Dell Inspiron 1721 battery

Dell Inspiron 1300 Battery
Dell Inspiron 1520 Battery
Dell Latitude D600 Battery
Dell XPS M1330 battery
Dell Latitude D531N Battery
Dell INSPIRON 6000 battery
Dell INSPIRON 6400 Battery
Dell Inspiron 9300 battery

Unknown said...

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

Unknown said...

I really like your writing style. Nice Post keep it up.

Asus - 15.6" Laptop - 3GB Memory - 320GB Hard Drive - Brown

Asus - 15.6" Refurbished Laptop - 4GB Memory - 500GB Hard Drive - Matte Dark Brown Suit

The Marketer said...

I enjoyed your blog Thanks for sharing such an informative post. We are also providing the best services click on below links to visit our website.

digital marketing company in nagercoil
digital marketing services in nagercoil
digital marketing agency in nagercoil
best marketing services in nagercoil
SEO company in nagercoil
SEO services in nagercoil
social media marketing in nagercoil
social media company in nagercoil
PPC services in nagercoil
digital marketing company in velachery
digital marketing company in velachery
digital marketing services in velachery
digital marketing agency in velachery
SEO company in velachery
SEO services in velachery
social media marketing in velachery
social media company in velachery
PPC services in velachery
online advertisement services in velachery
online advertisement services in nagercoil


Thanks for Sharing - ( Groarz branding solutions )

moumita said...

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