Optimizing code

There was an interesting discussion on /. yesterday. A guy wanted to know if people had any small code snippets they could share that did fairly common things in the fastest possible way. He wanted to make sure his code ran as fast as possible. Watching the following discussion was a very positive experience. Here are some quotes that sum up the general conclusion. The first quote I use a lot myself.
Premature optimization is the root of all evil
-- Tony Hoare's dictum.

The following quote contains the essence of most of the responses:
1) Code for maintainability
2) Profile your code
3) Optimize the bottlenecks

Many replies were also pointing out that picking the right algorithm is much more important that micro optimizing the code.
For 99% of the coders out there, all that needs to be known about code optimization is: pick the right algorithms!

Here's another beautiful quote:
"Programs should be written for people to read, and only incidentally for machines to execute."
- Structure and Interpretation of Computer Programs

I'm very happy to see this level of maturity from the /. crowd. I think there has been a shift the last five years or so. Before then too many programmers were concerned with speed. I think the reason for this shift is three fold:

1) Programs have become much more sophisticated. To be able to develop these programs one simply have to write maintainable code.
2) Computers have become much faster. You don't need to optimize your program to death to get decent performance out of it. It's OK to spend some clock cycles to get maintainable code.
3) today's CPUs are so complex that it's difficult to tell which piece of assembly code will run fastest. I'm mostly thinking of Intel's processors here with out of order execution, register renaming and multiple pipelines. Not to mention the cache hierarchy. It is nearly impossible to tell what the optimal assembly code for a given task should look like.

I very much welcome this attitude towards programming. Maybe we can start focusing on writing correct software now.

No comments: