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!

13 comments:

Fatih Co┼čkun said...

I believe; starred.

artsrc 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/

yi 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

dainfo said...

workout at home with dumbbells
at home workouts
lose weight at home
abs workout at home
exercise machines at a gym
chest workout at home
get fit with exercise
how to exercise at home
run a marathon to shed pounds and improve cardio
how to lose weight at home
a healthy heart comes with exercise
medicine ball abs
fatigue your muscles by using weights or your body
upper body workout at home
be sure to hydrate your muscles as often as possible
dandruff home remedy
Good share you have here hope you keep it up, and don't forget to use dumbbells while exercising, even better though you can do these lose weight at home
where to buy venapro
buy venapro
venapro
venapro reviews
venapro review
best tooth whitening system
bleaching your teeth
teeth bleaching products
teeth bleaching trays
tooth whitening bleach

Young said...

These onitsuka tiger online shop are buy asics tigers online awesome, tiger asics shoes but huge! I cheap onitsuka tiger had to asics tiger australia return twice cheap onitsuka tiger shoes because they onitsuka tiger zodiac were so big asics tiger I normally onitsuka tiger australia wear a 7.5 but ended up fitting asics onitsuka tiger mexico nicely into a tiger mexico 66. Other than that, onitsuka tiger shoes australia they look spiffy, seem onitsuka tiger mexico 66 black well made tiger mini cooper only had asics onitsuka so far, and they tiger shoes australia are still holding buy onitsuka tiger shoes online together mexico 66 perfectly, onitsuka tiger yellow black as they should be), and onitsuka tiger tokyo they came onitsuka tiger ultimate 81 with two asics onitsuka tiger trainers colors of laces asics kinsei. I onitsuka tiger mexico 66 yellow black would certainly tiger shoes onitsuka buy other onitsuka tiger sale colors of discount asics running shoes that I know onitsuka tiger mexico 66 my correct onitsuka tiger black white this brand buy onitsuka tiger shoes is a good asics international shipping best online onitsuka tiger online store

Cheapmonclerjacket said...

I would like to thank you for the efforts you have made in writing this post. I am hoping the same best work from you in the future as well. In fact your creative writing abilities has inspired me to start my own blog now.
There are many brand from France, also including herve leger, and most of womens stars love wearing herve leger dress when they join in some important party. Now polo ralph lauren is very popular with youthful people, everyone want to get ralph lauren polo shirts, there are lots of online shop which are ralph lauren polo outlet, true religion jeans outlet, it will be convenient for us.

Ethan Kleeberg said...

Wow! This can be one particular of the most beneficial blogs we have ever arrived across on this subject. Actually Magnificent. I am also a specialist in this topic so I can understand your hard work.

Computer Accessories | buy webcam

Berrty Gawill 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

Jerry Gene 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