“Avoid Premature Optimization” Does Not Mean “Write Dumb Code”
Programming in the 21st Century - James Hague - August 17, 2011First there’s a flurry of blog entries citing a snippet of a Knuth quote: “premature optimization is the root of all evil.” Then there’s the backlash about how performance needs to be considered up front, that optimization isn’t something that can be patched in at the end. Around and around it goes.
What’s often missed in these discussions is that the advice to “avoid premature optimization” is not the same thing as “write dumb code.” You should still try to make programs clear and reliable and factor out common operations and use good names and all the usual stuff. There’s this peculiar notion that as soon as you ease up on the hardcore optimization pedal, then you go all mad and regress into a primitive mindset that thinks BASIC on an Apple ][ is the epitome of style and grace.
The warning sign is when you start sacrificing clarity and reliability while chasing some vague notion of performance.
Imagine you’re writing an application where you frequently need to specify colors. There’s a nice list of standard HTML color names which is a good starting point. A color can be represented as an Erlang atom: tomato, lavenderBlush, blanchedAlmond. Looking up the R,G,B value of a color, given the name, is straightforward:
color(tomato) -> {255,99,71};
color(lavenderBlush) -> {255,240,245};
color(blanchedAlmond) -> {255,235,205};
...
That’s beautiful in its textual simplicity, but what’s going on behind the scenes? That function gets turned into the virtual machine equivalent of a switch statement. At load time, some additional optimization gets done and that switch statement is transformed into a binary search for the proper atom.
What if, instead of atoms, colors are represented by integers from zero to some maximum value? That’s easy with macros:
-define(Tomato, 0). -define(LavenderBlush 1). -define(BlanchedAlmond, 2). ...
This change allows the color function to be further, automatically, optimized at load time. The keys are consecutive integers, so there’s no need for a search. At runtime there’s a bounds check and a look-up, and that’s it. It’s hands-down faster than the binary search for an atom.
What’s the price for this undefined amount of extra speed? For starters, colors get displayed as bare integers instead of symbolic names. An additional function to convert from an integer to a name string fixes that…well, except in post-crash stack traces. The easy-to-read and remember names can’t be entered interactively, because macros don’t exist in the shell. And the file containing the macros has to be included in every source file where colors are referenced, adding dependencies to the project that otherwise wouldn’t be present.
The verdict? This is madness: sacrificing ease of development, going against the grain of Erlang, all in the name of nanoseconds.
(If you liked this, you might enjoy Two Stories of Simplicity.)
Categories: Blogs Programming in the 21st Century
Comments
No comments so far, you could be the first.Add comment
Erlang on Twitter
» HornedKavu (Max Riveiro): RT @shwars: Вчера в докладе career.ru на #itedu были озвучены новые востреб.профессии на IT-рынке: Haskell-, Lisp- и Erlang-программист!
» phyrexianengine (Vasily K.): RT @shwars: Вчера в докладе career.ru на #itedu были озвучены новые востреб.профессии на IT-рынке: Haskell-, Lisp- и Erlang-программист!
» Erlang_ABNIC (Erlangga .A): @cjerikho829 selamat shooting ♈o kak.. Nitip salam buat kak @Bellaudya829.. ☺ Semangat (งˆヮˆ)ง
» dessyrosalia (♡pesek mancung♥ ): Erlang ke rumahku donk kangen nih
» si_erlang (Erlangga Adhitya): 75% dalane jahanam
» GeekDani (Dani Kim): @charsyam 그렇군용. :-) 여긴 서늘한데. 크크. Erlang Meetup 준비는 잘 하시나요. ㅋㅋ
» syahlafatimahA (LalaTik(ʃ⌣ƪ) ): Waaa?! Si erlang suka cherrybelle(?) wkwkwkwk ngakak aih xD
» yosukehara (Yosuke Hara): I’ll be a simple test for benchmarking JSX and Jiffy together. #erlang
» Debbyvheumen (Debby van Heumen): @elisaaa15 @kleingeld_ haha okee succes :) blijven jullie erlang
» ovatsus (Gustavo Guerra): RT @martintrojer: Just *blogged “Distributed Actors in Clojure” on http://t.co/WcKBpNBR #Clojure #Akka #Erlang #in
Statistics
Number of aggregated posts: 10498
Number of comments: 2115
Most recent article: May 15, 2012
Latest comments
» cheap soccer jerseys on Memory Models in Erlang vs Java: Nice discussion here,you are doing a great job. i was looking for this information. i found it on your page…
» mandesejohn on Couchbase Meetup at new HQ: Thanks for sharing experience. It should be really a great post. It should be knowledgeable and informative. Keep it up. flower delivery columbus ohio
» vermaseo on Scale means Skills: I’m surprised people are still commenting about this. George has been moved on to bigger and better things with the president for awhile now.ledikanten