Code Optimization | Code Optimization Techniques

What is Code Optimization?

 

Code Optimization is an approach for enhancing the performance of the code by improving it through the elimination of unwanted code lines and by rearranging the statements of the code in a manner that increases the execution speed of the code without any wastage of the computer resources.

 

Advantages of Code Optimization-

 

  • Optimized code has faster execution speed
  • Optimized code utilizes the memory efficiently
  • Optimized code gives better performance

 

Techniques for Code Optimization-

 

Important code optimization techniques

 

  1. Compile Time Evaluation
  2. Common sub-expression elimination
  3. Dead Code Elimination
  4. Code Movement
  5. Strength Reduction

 

1. Compile Time Evaluation-

 

Two techniques that falls under compile time evaluation are-

 

A) Constant folding-

 

  • As the name suggests, this technique involves folding the constants by evaluating the expressions that involves the operands having constant values at the compile time.
  • Example-

Circumference of circle  = (22/7) x Diameter

Here, this technique will evaluate the expression 22/7 and will replace it with its result 3.14 at the compile time which will save the time during the program execution.

 

B) Constant Propagation-

 

  • In this technique, if some variable has been assigned some constant value, then it replaces that variable with its constant value in the further program wherever it has been used during compilation, provided that its value does not get alter in between.
  • Example-

pi = 3.14

radius = 10

Area of circle = pi x radius x radius

Here, this technique will substitute the value of the variables ‘pi’ and ‘radius’ at the compile time and then it will evaluate the expression 3.14 x 10 x 10 at the compile time which will save the time during the program execution.

 

2. Common sub-expression elimination-

 

The expression which has been already computed before and appears again and again in the code for computation is known as a common sub-expression.

As the name suggests, this technique involves eliminating the redundant expressions to avoid their computation again and again. The already computed result is used in the further program wherever its required.

 

Example-

Code before Optimization Code after Optimization
S1 = 4 x i

S2 = a[S1]

S3 = 4 x j

S4 = 4 x i  // Redundant  Expression

S5 = n

S6 = b[S4] + S5

S1 = 4 x i

S2 = a[S1]

S3 = 4 x j

S5 = n

S6 = b[S1] + S5

 

3. Code Movement-

 

As the name suggests, this technique involves the movement of the code where the code is moved out of the loop if it does not matter whether it is present inside the loop or it is present outside the loop.

Such a code unnecessarily gets executed again and again with each iteration of the loop, thus wasting the time during the program execution.

 

Example-

Code before Optimization Code after Optimization
for ( int j = 0 ; j < n ; j ++)

{

x = y + z ;

a[j] = 6 x j;

}

x = y + z ;

for ( int j = 0 ; j < n ; j ++)

{

a[j] = 6 x j;

}

 

4. Dead code elimination-

 

As the name suggests, this technique involves eliminating the dead code where those statements from the code are eliminated which either never executes or are not reachable or even if they get execute, their output is never utilized.

 

Example-

Code before Optimization Code after Optimization
i = 0 ;

if (i == 1)

{

a = x + 5 ;

}

i = 0 ;

 

5. Strength reduction-

 

As the name suggests, this technique involves reducing the strength of the expressions by replacing the expensive and costly operators with the simple and cheaper ones.

 

Example-

Code before Optimization Code after Optimization
B = A x 2 B = A + A

 

Here, the expression “A x 2” has been replaced with the expression “A + A” because the cost of multiplication operator is higher than the cost of addition operator.

 

To gain better understanding of the concepts of Code Optimization Techniques,

Watch this Video lecture

Download the handwritten notes of “Code Optimization Techniques” here-

Get more notes and other study material of Compiler Design.

Watch video lectures by visiting our YouTube channel LearnVidFun.

Summary
Code Optimization | Code Optimization Techniques
Article Name
Code Optimization | Code Optimization Techniques
Description
In compiler design, Code Optimization is an approach for enhancing the performance of the code by employing various code optimization techniques such as- Compile Time Evaluation, Common Sub expression elimination, code movement, dead code elimination, strength reduction.
Author
Publisher Name
Gate Vidyalay
Publisher Logo
Liked this article? Share it with your friends and classmates now-