Thursday, November 11, 2004

Optimizations

On one of the mass mailing lists that I moderate (people subscribe to them - not SPAM), I constantly see people asking about whether something like memset() is faster than assigning variables the values directly. These are trivial optimizations and usually someone says so.

Given that today's computers are averaging around 2GHz in the user realm, does is not seem odd that such discussions are still going on? What is typically happening is that the person read somewhere that optimizations for performance are really important. The person ends up trying to optimize code that simply doesn't need optimization or they are trying to optimize the code because they used the wrong algorithm for the given situation. The only traditional bottleneck where optimizing code to extremes was where the algorithm was the correct one to use and just happened to be performance intensive. This happened particularly when developing games. Of course, if you were developing games, you were in a whole other league of programming anyway doing all sorts of cutting-edge stuff. Even then, most optimizations were restricted to graphics display routines done in assembler.

For the rest of the population of programmers, we have what is called code maintenance. Code maintenance accounts for 60% of any given project's cost. You should be far more concerned with writing maintainable code than code that pulls cute tricks to do some optimization. If someone is going to come through the code later and wonder what in the world you did, don't write it. That person will re-write what you did and do a better job of it. If you care about job security, write maintainable code. That sounds ironic to some people because it seems that it would make you less valuable, but instead it makes you a better part of the team. If everyone on the team does the same thing, you can edit each other's code if you have to, but you won't have to because smart bosses know about the joys of code ownership. Your boss, if he/she is smart will let you "own" your code by letting you be the one to make, edit, and maintain it. So, you want to also make your own code maintainable so that you enjoy maintaining it.

Now, that does not mean you will never write super-crazily-optimized code. It just means that when the time comes to optimize code, you will be able to write optimized code in a maintainable fashion.

No comments:

Post a Comment