Code Optimization
Code Optimization
Optimized code:
y = x + 5;
w = y * 3; //* there is no i now
//* We've removed two redundant variables i & z whose value were just being copied
from one another.
B. Constant folding: The code that can be simplified by the user itself, is simplified.
Here simplification to be done at runtime are replaced with simplified code to avoid
additional computation.
Initial code:
x = 2 * 3;
Optimized code:
x = 6;
C. Strength Reduction: The operators that consume higher execution time are
replaced by the operators consuming less execution time.
Initial code:
y = x * 2;
Optimized code:
y = x + x; or y = x << 1;
Initial code:
y = x / 2;
Optimized code:
y = x >> 1;
D. Null sequences/ Simplify Algebraic Expressions : Useless operations are
deleted.
a := a + 0;
a := a * 1;
a := a/1;
a := a - 0;
E. Combine operations: Several operations are replaced by a single equivalent
operation.
F. Deadcode Elimination:- Dead code refers to portions of the program that are
never executed or do not affect the program’s observable behavior. Eliminating dead
code helps improve the efficiency and performance of the compiled program by
reducing unnecessary computations and memory usage.
Initial Code:-
int Dead(void)
{
int a=10;
int z=50;
int c;
c=z*5;
printf(c);
a=20;
a=a*10; //No need of These Two Lines
return 0;
}
Optimized Code:-
int Dead(void)
{
int a=10;
int z=50;
int c;
c=z*5;
printf(c);
return 0;
}
After optimization:
t = Sin(x)/Cos(x);
while(i<100)
{
a = t + i;
i++;
}
If the value of any variable in any loop gets changed every time, then such a variable is
known as an induction variable. With each iteration, its value either gets incremented or
decremented by some constant value.
Example:
Before optimization:
B1
i:= i+1
x:= 3*i
y:= a[x]
if y< 15, goto B2
In the above example, i and x are locked, if i is incremented by 1 then x is incremented
by 3. So, i and x are induction variables.
After optimization:
B1
i:= i+1
x:= x+4
y:= a[x]
if y< 15, goto B2
3. Strength Reduction
Strength reduction deals with replacing expensive operations with cheaper ones like
multiplication is costlier than addition, so multiplication can be replaced by addition in
the loop.
Example:
Before optimization:
while (x<10)
{
y := 3 * x+1;
a[y] := a[y]-2;
x := x+2;
}
After optimization:
t= 3 * x+1;
while (x<10)
{
y=t;
a[y]= a[y]-2;
x=x+2;
t=t+6;
}
In the loop invariant method, the expression with computation is avoided inside the loop.
That computation is performed outside the loop as computing the same expression
each time was overhead to the system, and this reduces computation overhead and
hence optimizes the code.
Example:
Before optimization:
for (int i=0; i<10;i++)
t= i+(x/y);
...
end;
After optimization:
s = x/y;
for (int i=0; i<10;i++)
t= i+ s;
...
end;
5. Loop Unrolling
Loop unrolling is a loop transformation technique that helps to optimize the execution
time of a program. We basically remove or reduce iterations. Loop unrolling increases
the program’s speed by eliminating loop control instruction and loop test instructions.
Example:
Before optimization:
After optimization:
printf("Pankaj\n");
printf("Pankaj\n");
printf("Pankaj\n");
printf("Pankaj\n");
printf("Pankaj\n");
6. Loop Jamming
Loop jamming is combining two or more loops in a single loop. It reduces the time taken
to compile the many loops.
Example:
Before optimization:
After optimization: