![]() |
VOOZH | about |
Code optimization is the process of improving a program to make it more efficient in terms of speed, memory, and resource usage, without changing its functionality. The key aspects of code optimization include:
Optimization of the code is often performed at the end of the development stage since it reduces readability and adds code that is used to increase performance.
Algorithm optimization is beyond the scope of the code optimization phase. Instead, the compiler optimizes the intermediate program code, which may also reduce the size of the generated code. Therefore, code optimization helps to:
There are several ways to optimize code. Some of them are mentioned below.
Expressions evaluated during compilation.
Replaces a variable with another variable having the same value.
Replaces variables with their constant values.
Example:
Computes constant expressions at compile time.
Example:
Example :
Avoids repeated calculation of the same expression.
Removes code that is never used.
Example:
Removes code that will never execute.
Replaces a function call with its body.
Uses variables that change regularly in loops.
Examples:
Replaces costly operations with cheaper ones.
This technique reduces the number of times an expression is evaluated by moving loop-invariant statements outside the loop.
Example:
Loop Jamming combines two or more loops into a single loop to reduce loop overhead and improve runtime performance.
Example:
This technique reduces the number of loop iterations by performing multiple operations in a single iteration, which decreases loop control overhead.
Example:
Code optimization can be applied at different stages of program execution and compilation to improve performance and efficiency.
1. Source Program Level
Optimization at the source program level involves modifying the algorithm or program structure, such as improving loop structures or reducing unnecessary computations. Here, the programmer (user) performs the optimization.
2. Intermediate Code Level
At this stage, the compiler optimizes the intermediate representation of the program. This may include improving address calculations and optimizing procedure calls.
3. Target Code Level
Optimization is performed on the generated machine code. The compiler improves the use of CPU registers, instruction selection, and instruction movement to produce efficient target code.
| Type | Description | Techniques Used |
|---|---|---|
| Local Optimization | Applied to small basic blocks of statements. | Local Value Numbering, Tree Height Balancing |
| Regional Optimization | Applied to Extended Basic Blocks. | Super Local Value Numbering, Loop Unrolling |
| Global Optimization | Applied to large program sections such as functions and loops. | Live Variable Analysis, Global Code Replacement |
| Interprocedural Optimization | Applied across multiple procedures or functions. | Inline Substitution, Procedure Placement |