Blogs

News

Favorite Posts

ctodx

Discussions, news and rants from the CTO of Developer Express, Julian M Bucknall

DevExpress Newsletter 39: Message from the CTO

     

Just a quickie thought this time around, not too heavy:

Avoiding micro-optimizations

What's a micro-optimization, you may ask? It's applying a local-in-scope rule about optimization to your code without actually testing to see if it makes any difference to the performance of your application as a whole. Micro-optimizations are generally cast as never-to-be-broken rules, or even as items in some coding standards document.

Sometimes we refer to them as premature optimizations. The root of all evil, to quote Knuth.

Examples are easy to find: always using for instead of foreach, appending to a StringBuilder object instead of string concatenation, avoiding small methods to minimize the call time.

Now, before you start huffing and puffing, I'm not advocating abandoning these optimizations, or saying that they are inherently evil, or anything like that. All I'm saying is that you should initially write code to be as clear as possible. That implies that your code should be readable, simple and understandable. Writing clear code gives you the best chance of creating provably correct software and of creating code that is easier to optimize. Writing clear code provides the best opportunity for enhancing maintainability. Clear code is generally more flexible and better able to adapt to future needs and changes. Consider using refactorings guided by software metrics to simplify your code. Consider using your skills as a developer to identify and remove code smells.

And it's only after your code is written that you get to profile it to test its performance within the application. Only then do you get to analyze the hot spots in your application as a whole. Only then do you get to formulate a thesis ("replacing this simple-to-read foreach with a slightly harder to read for loop will improve the performance of my application as a whole") and then test it with the real application. Only then should you accept code changes that have a measurable change in your application's performance. Only then should you reject failed theses and revert your code to the simpler-to-read version.

Don't change your code to suit some optimization fad: change it to improve its performance.

Having written it, I now wonder: when do micro-optimizations become so familiar that they are clear and readable? As an example, I would venture to say that using a StringBuilder is actually more legible than writing a set of statements that do string concatenation. Or someInt >> 1 becomes just as readable as someInt / 2. Hmm.

Published Dec 20 2010, 07:00 AM by Julian Bucknall (DevExpress)
Filed under:
Technorati tags: Newsletter
Bookmark and Share

Comments

 

Geoffrey Hirst said:

Never really heard of the term Micro-optimizations, but have come across 'experts' who just 'know' that it must be done their way as it shaves 0.02 nanoseconds off the time it takes to do 'x'.

Personally, I let my audience decide, if they think something is slow, they'll tell me and from there I will investigate as to why. Sometimes there might be an evil crab in there, sometimes it just might be the fact that they are still running XP with 512MB ram and that cannot really be helped with any optimizations, can it?

Compliments of the season Julian.

December 21, 2010 3:50 AM

About Julian Bucknall (DevExpress)

Julian is the Chief Technology Officer at Developer Express. You can reach him directly at julianb@devexpress.com. You can also follow him on Twitter with the ID JMBucknall.
More from DevExpress
Live Chat
Have a pre-sales question?
Need assistance with your evaluation?
We are here to help.
Chat is one of the many ways you can contact members of the DevExpress Team. We are available Monday-Friday between 8:30am and 5:00pm Pacific Time.
If you need additional product information, require pre-sales assistance, or want help with your order, write to us at info@devexpress.com or call us at
+1 (818) 844-3383.