April
2006
The sweet smell of optimization0
Developers love to optimize. They love it so much they will often engage in it too early, a habit so bad it will frequently back-fire. But even the mature ones will happily pursue that ultimate sweet goal – making that code run faster or use less bandwidth, or consume less space. We all want that warm fuzzy feeling of achieving 53% improvement, and then 15% and then, pathetically, the last 2.71% .
It is probably due to the immediate and accurate feedback that optimization gives us. It is measurable and involves no smart-ass manager’s opinion or some client’s subjective impression. It is the perfect ego booster. It is also an enjoyable process during which we can test some tricks and resurrect our old passion for data-structures 101.
The flip side of it is bad, ugly, non-reusable code. That is, most of the time optimization and especially micro-optimization, will result in transforming the naively elegant structure of the code into a hairy beast that runs faster but smells. A harsh fact. I was always amused to see that programmers write “dense” code when trying to optimize a lot. That is, for some odd reason the variables names are always a single letter, with less spaces per line sometimes even fitting multiple statements into a single semi-colon separated line. As if by taking less space the code would run faster.
Unless of course you have meditated on it long enough to understand that there is another, even more elegant way of solving the problem at hand. And not only is it elegant it is simpler and faster. This is macro-optimization and is the true reward of programming—solving a problem the second time after reaching enlightenment. Sweet indeed.