“We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil.”
-Knuth
This post can be summed up in the following sentance. ‘Make sure you can justify each and every optimization of your code, before you make the change’. Stated another way ‘If you haven’t profiled your code, then you don’t know what to optimize’.
Now for the justification…
In my career I’ve done everything from QA, to Development, to Developer Support at Borland. In that time I’ve seen a lot of funky code. By funky, I don’t mean strange indentation, or variable naming, but code that jumps through convoluted hoops in the name of ‘efficiency’. I may be overly sensitive to this, as I saw a lot of this when I was working in support (people would contact us for help after they couldn’t fix the bugs in their own code, or sometime ours).
I will say that this type of problem seems to be much more prevalent with C/C++ programmers than Java programmers. I think this stems from the fact that most Java programmers don’t really think about memory management and performance when coding, but in their defense, they don’t normally have to. But I digress…
Most of the hackery that I’ve seen in regards to performance isn’t really effective. Sure, optimizing that long running startup code brings up the program faster, but since that only happens once every N number of hours, it’s not making a big difference overall. Programmers will see some area in their code where they can use a new and sexy data structure or technique to speed things up, and will do so, justifying it as optimizing.
In almost every case that I’ve seen, programs spend the vast majority of their time in a very small portion of their code. Optimizing that portion of code, no matter how un-sexy, is where the big gains will come from. Also, that portion of the code base should have the best set of unit tests, so you have that much more assurance that you haven’t inadvertently introduced a bug.
Also, a lot of times the bottleneck isn’t in your code directly, but in a library or downstream dependency. If it takes 2 seconds for for your query to return results, then you need to make sure your tables have the correct indexes, add caching, etc. *D0 Not* fork the hibernate code your using to try and eek out a little more performance.
So please, justify with an estimate of the performance gain of your proposed optimization before you start modifying the code. If you have no idea what the gain will be, then you probably don’t understand how/why the code is slow well enough to start making changes effectively.
Mat