I discovered a terrific blog post on code optimization, through my friend Mars’s blog.
To summarize in my own words, the most important parts are:
- Start with unit tests, so you can be confident that the code worked in the first place, and that you haven’t broken anything in the optimization process.
- Never assume you know what the bottleneck is, even if it’s “obvious”. Profile first, code later.
- Reprofile after every code change, so you know whether the remaining efficiency issues are still in the same place, or if a new spot is the current big problem.
There are two more points in the post, but they are either obvious or much less important, IMHO.