Program optimization is the process of modifying a computer program in a way that causes the program to execute faster, consume fewer resources or generally function with a higher level of efficiency. The task of optimization can be performed automatically by some programming language compilers, intentionally by using an optimization program, or manually by programmers who step through source code and attempt to make specific improvements. In general, program optimization is performed with a specific end in mind, because there are very few general optimizations that can be made to a program that do not in some way reduce the optimized state of another part of the program, meaning a program usually can be optimized for speed or resource usage, but usually not both. One complication that can occur with some types of optimization is that many high-level programming languages provide such a large level of abstraction between native code and the computer language that optimization can be difficult or impossible to implement across all platforms in all situations, especially with interpreted languages that use just-in-time (JIT) compilation.
An important concept in program optimization is the idea that an optimization usually comes with some kind of price. One example of this is that, when a piece of code is optimized to run more quickly, the increase in speed could come at the price of code readability, memory usage, program flexibility or a number of other costs. This means program optimization must be a targeted process, with the intention of making one aspect of a program operate in a better way while being willing to sacrifice the efficiency of other aspects.
Different types of program optimization can be performed at different stages of program development. During design, broad optimization can be done by ensuring a program appears to run effectively. When working with actual source code, optimizations can include ensuring there are no extraneous commands, repetitive calls or poorly written functions. At compilation, many optimizations are automatically performed by the compiler and can be guided through the use of different compiler switches or directives by the programmer.
Automatic optimizations, as can occur with a compiler or dedication optimization program, often can involve tricks that are too complex to be practical for human programmers. This can involve moving instructions in a program so they are executed out of the order originally written but in a more efficient way for the processor. It also can involve intentionally shifting resources such as memory blocks so they can be accessed faster. Most program optimization occurs automatically at the compiler level.
One complication with repeated or aggressive program optimization is that, once a program has been modified to run more efficiently, it generally becomes more difficult to modify for other purposes, such as adding functionality or fixing bugs. This can occur when optimizations start to lock in set program behaviors that are not easily changed or adapted to new code without requiring that all optimizations be undone. A larger issue is that, in many cases, an optimized program becomes less human-readable, because tricks and shortcuts are used instead of only concise commands and classical control structures. For these reasons, there often is a level of program optimization at which it is acceptable to stop, even though drastic code modifications might make a program work slightly more efficiently.