usable in any place a human can be used

20100331

micro-optimization

[caption id="attachment_818" align="alignright" width="277" caption="Yea, I know it\'s on fire, but check it, I almost got Freebird down pat."]nero fiddling as rome burns[/caption]

As all good developers should know, one of the cardinal sins of software development is premature optimization. Premature optimization is bad for a whole host of reasons. But there is another set of optimizations that are in the same realm of bad, micro-optimizations.


I recall learning at some point that in C and C++ it is more efficient to use the pre-increment operator rather than the post-increment operator. Why is this the case, well it's because of the minor difference in behavior between the following two snippets.


[cpp]
int source = 10;
int destination = 0;

destination = ++source;

printf("Destination: %d", destination); //Prints "Destination: 11"
[/cpp]

Compare with this snippet


[cpp]
int source = 10;
int destination = 0;

destination = source++;

printf("Destination: %d", destination); //Prints "Destination: 10"
[/cpp]

WHA!? This is actually exactly what we would expect, pre-increment increments the value BEFORE assignment while post-increment increments the value AFTER assignment. What does this all mean, well basically that if you use post-increment you are executing more instructions, because the compiler has to keep the old value around to return to the assignment. That means that unless you are assigning the value and require the particular behavior that post-increment gives you, you can generate faster code by using the pre-increment operator (this may be handled by compiler optimizations these days).


All of the for-loops that you've written for(int i = 0; i < something; i++) are identical to and SLOWER than for(int i = 0; i < something; ++i). NOOOOO!!!!! Don't worry though, because this is a micro-optimization, its something to not care about, because in reality that one or two extra machine instructions isn't your bottleneck. Micro-optimizations are all those tricks that make code faster that in the vast majority of cases (although not all cases) don't really amount to any actual noticeable performance gain. A new processor can do something in the magnitude of 100,000 MIPS (Millions of Instructions Per Second). Every second it does 100,000,000,000 Instructions, that is 100 billion instructions. every. single. second. Changing from post- to pre- increment saves 1 or 2 instructions, so for every time that gets executed you have saved a 100th of a nanosecond.


Micro-optimizations are rarely worth the hassle, and, as with premature optimization, the solution is to benchmark and trace where your actual bottlenecks are and go about solving those. It doesn't matter if you're screaming through your increments saving a whole nanosecond if your crappy no index having table is taking a whole minute to read because you are doing a table-scan.


But wait, there's more. Micro-optimization, like everything in programming, can be directly applied to life in general. I work with a person who will spend 5 minutes discussing how to save 30 seconds, this is another incarnation of the micro-optimization. It's that shortcut you take home that actually takes longer, or some everyday voodoo that is draining your time without paying you back, or that client that provides you with 2% of your income but eats up 40% of your effort. Micro-optimizations abound all around us, in the little picture they seem ok, but in the big picture they are nonsense. Take a fresh look at the things and obligations around you and examine them for micro-optimizations, see if the things you do make sense in the big picture.

3 comments:

  1. The best part about most micro-optimizations is that they don't save you anything in the long run. Sure, ++turd_count saves 1 or 2 operations, but when you're talking about memory taking 10+ operations to get from cache to the CPU in a modern multi-core processor, is it really worth it?

    Why not, I don't know, buy some crullers and chill?

    ReplyDelete
  2. Beyond the 30 seconds saved, maybe you're coworker just wants something to talk about :)

    ReplyDelete
  3. [...] by recent reads over JRuby tweaks and micro optimizations today’s riff will be about a different design [...]

    ReplyDelete