Program optimization can be defined as follows:
Given a program P, produce a program P' that produces the same output values as P for a given input, but has a lower cost.
Typical costs are execution time and program space. Most optimizations target time; fortunately, the two usually go together.
Optimization is an economic activity:
Amount saved by the code improvement * number of occurrences in code * number of repetitions in execution * number of uses of the compiled code