Code Optimization
Code Optimization
- Shilpa Kalantri
Code Optimization-
Advantages-
A) Constant Folding-
In this technique,
● As the name suggests, it involves folding the constants.
● The expressions that contain the operands having constant values at compile time are
evaluated.
● Those expressions are then replaced with their respective results.
Example-
Here,
● This technique evaluates the expression 22/7 at compile time.
● The expression is then replaced with its result 3.14.
● This saves time at run time.
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 during compilation.
● The condition is that the value of the variable must not get altered in between.
Example-
pi = 3.14
radius = 10
Area of circle = pi x radius x radius
Here,
● This technique substitutes the value of variables ‘pi’ and ‘radius’ at compile time.
● It then evaluates the expression 3.14 x 10 x 10.
● The expression is then replaced with its result 314.
● This saves time at run time.
Example-
Before optimization
N = 10; C = 2;
for ( i = 0; i < N; i++ )
{
s = s + i*C;
}
After optimization
for ( i = 0; i < 10; i++ )
{
s = s + i*2;
}
The expression that has been already computed before and appears again in the code for
computation is called as Common Sub-Expression.
In this technique,
● It involves eliminating the common sub expressions.
● The redundant expressions are eliminated to avoid their re-computation.
● The already computed result is used in the further program when required.
Example-
S1 = i + j S1 = i+j
S2 = p+q S2 = p+q
S3 = i+j+p+q S3 = S1+S2
3. Code Movement-
In this technique,
● As the name suggests, it involves movement of the code.
● The code present inside the loop is moved out if it does not matter whether it is present inside
or outside.
● Such a code unnecessarily gets executed again and again with each iteration of the loop.
● This leads to the wastage of time at run time.
Example-
Code Before Optimization Code After Optimization
x=y+z; {
a[j] = 6 x j; a[j] = 6 x j;
} }
Example-
do
{
item = 10;
value1 = value + item;
} while(value<100);
item = 10;
do
{
value1 = value + item;
} while(value<100);
4. Dead Code Elimination-
In this technique,
● It involves eliminating the dead code.
● The statements of the code which either never executes or are unreachable or their output is
never used are eliminated.
Example-
i=0;
if (i == 1)
{ i=0;
a=x+5;
}
Before optimization
int add(int x, int y)
{
int i=0, z;
if( i==1)
{
z = x+i;
}
z=x+y;
return z;
printf(“%d”, z);
}
After optimization
int add(int x, int y)
{
int z;
z=x+y;
return z;
}
5. Strength Reduction-
In this technique,
● It involves reducing the strength of expressions.
● This technique replaces the expensive and costly operators with the simple and cheaper ones.
Example-
B=Ax2 B=A+A
Here,
● The expression “A x 2” is replaced with the expression “A + A”.
● This is because the cost of the multiplication operator is higher than that of the addition
operator.