Chapter 11: THE NEED FOR SPEED Optimizing Programs and Writing Efficient Code
- Think about the performance of your program from the very start—do not ignore it, hoping to make quick fixes at the end of development.
- Correct code is far more important than fast code. There’s no point in arriving quickly at the wrong answer.
- Look for alternatives to optimizing code—can you increase your program’s performance in any other way?
- Understand when you do need to optimize code, but prefer to write efficient high-quality code in the first place.
- Optimize your code separately from any other work, so the outcome of one task doesn’t cloud the other.
- Optimize release builds of your program, not development builds.
- Tip: Test an individual function’s impact on the total program execution time by making it slower. If you suspect that a particular function is causing a slowdown, try replacing its call with two calls in succession, and measure how it affects execution time. If the program takes 10 percent longer to run, then the function consumes approximately 10 percent of execution time. Use this as a very basic timing test.
- Don’t rely solely on a profiler to find the causes of program inefficiency; you might miss important problems.
- Never try to optimize code without performing some kind of before and after measurement.
- Prefer to replace a slow algorithm with a faster variant than to tinker with the algorithm’s implementation.
Jun 30, 2010
comments powered by Disqus