Lecture Notes for Advanced Programming II
1 March 2001 - Making Code Efficient
- there are two kinds of efficiency - conceptual and execution
- write conceptually efficient code
- then, if necessary, change the code for execution efficiency
- c++ makes it easy and hard to write efficient code
- easy - access to low-level features
- hard - complexity, surprising but defined behavior, access to low-level
features
- c is easier, java is harder
- the compiler is smarter than you are
- you write conceptually simple code, let the compiler write code that
executes efficiently
- don't help the compiler -
register
, inline
- you don't know how your program is executing
- a program's detailed dynamic behavior cannot be predicted
- you must measure performance to know where to work
- writing efficient code is like debugging - find the inefficiencies and
fix
- you cannot make efficient inherently inefficient algorithms
- you must write your programs so they can be easily tuned
- efficient code is not simple
- making code efficient can be complicated
- simpler code is easier to make more efficient
- understand the code's static behavior
- you must understand what the code's supposed to do
- the code must be well structured and simple
- you can't outrun inefficient algorithms
- run experiments to determine the code's dynamic behavior
- you don't know why the code's inefficient
- use performance tools to measure the code - statement counts, execution
time, memory use
- change the code from inside out
- make the scope of the change as small as possible - 80-20 rule
- innermost loops, then procedures, then move out, . . .
- don't patch,
#ifdef
, special case - rewrite
- understand the performance of the code you're adding
- check your results
- is the code still correct?
- does the code perform better? significantly better?
- if so - stop
- if not, go back and try again
- if you can't get significant improvements, back out
This page last modified on 27 February 2001.