… or not to optimize, that is the programmer’s dilemma. Optimization is a powerful tool in the programmer’s arsenal, but one that should be used sparingly and with care. The key to optimization is identifying what exactly to optimize.
Shawn Hargreaves, creator of the Allegro game programming library recently posted a link to the blog of Thomas Aylesworth, a.k.a. SwampThingTom. According to his blog, Thomas is a software engineer for the aerospace and defense industry. He’s also a hobbyist game developer.
Tom’s first foray into the world of blogging is a series of posts about optimization, specifically targeting XNA based games. In his first post, he explains the importance of design and what Big O notation is. His second post delves into prototyping and benchmarking, complete with examples. In part three, he introduces us to the NProf execution profiler as well as pointing out a few other potential bottlenecks.
Optimization in general is something you shouldn’t really need to worry about until the very end of the development cycle, if ever. Optimization is a great tool for squeezing just a few more cycles out of your code when you really need it. What you normally don’t see, however is a significant increase in speed. If you’re looking for a significant increase in speed, take a look at your underlying algorithm.
Generally speaking, unless you’re writing extremely specialized code, optimization should be used very sparingly. You’re better off looking for the elegant way to solve your programming dilemma. Look into alternative algorithms or possibly re-design the data flow. If you’re not sure where the bottleneck is, look into using a code profiler, or simply add debugging statement that surround suspected “slow” code. You’ll usually find that a poor design decision is causing the bottleneck and that a simple re-design can result in huge speed increases while keeping your code readable and maintainable.
If you’re interested in optimization, or even just curious, take a look at Tom’s articles, they’re a great read.