Group
Group
Group
Software Engineering
Name…………………………………….Id
1.Lalisa Gutama………………………..Nsr/1688/20
2. Samuel Wasenu…………………….Nsr/1149/20
3. Abdi Abera………………………….Nsr/1951/20
4.Dagem zerihun………………………..Nsr/2164/20
5.Xahir Bakele………………………….Nsr/
Contents ………………………………………………………page
Correctness
Performance
Compilation time
Delay
When to Optimize?
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 the
performance.
Why Optimize?
C.
(i) A = 2*(22.0/7.0)*r
Perform 2*(22.0/7.0)*r at compile time.
(ii) x = 12.4
y = x/2.3
Evaluate x/2.3 as 12.4/2.3 at compile time.
Output:
Requesting error……
./Solution.c:1:5: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'A'
(i) A = 2*(22.0/7.0)*r
2. Variable Propagation:
C
//Before Optimization
c=a*b
x=a
till
d=x*b+4
//After Optimization
c=a*b
x=a
till
d=a*b+4
3. Constant Propagation:
If the value of a variable is a constant, then replace the variable with the
constant. The variable may not always be a constant.
Example:
C
(i) A = 2*(22.0/7.0)*r
(ii) x = 12.4
y = x/2.3
if(k) go to L3;
It is evaluated as :
4. Constant Folding:
#define k 5
x=2*k
y=k+5
This can be computed at compile time and the values of x and y are :
x = 10
y = 10
Function-Preserving Transformations
1. Copy Propagation:
//Before Optimization
c=a*b
x=a
till
d=x*b+4
//After Optimization
c=a*b
x=a
till
d=a*b+4
c=a*b
x=a
till
d=a*b+4
//After elimination :
c=a*b
till
d=a*b+4
The block which does not have an incoming edge is an Unreachable code block.
After constant propagation and constant folding, the unreachable branches can be
eliminated.
C++
#include <iostream>
int main() {
int num;
num=10;
return 0;
int main() {
int num;
num=10;
return 0;
}
5. Function Inlining:
Here, a function call is replaced by the body of the function itself.
This saves a lot of time in copying all the parameters, storing the return address,
etc.
Strength reduction means replacing the high strength operator with a low strength.
Examples:
Example 1 :
Multiplication with powers of 2 can be replaced by shift left operator which is less
a=a*16
// Can be modified as :
a = a<<4
Example 2 :
i = 1;
while (i<10)
y = i * 4;
}
//After Reduction
i=1
t=4
while( t<40)
y = t;
t = t + 4;
Example:
a = 200;
while(a>0)
b = x + y;
if (a % b == 0)
printf(“%d”, a);
a = 200;
b = x + y;
while(a>0)
if (a % b == 0}
printf(“%d”, a);
2. Loop Jamming:
Two or more loops are combined in a single loop. It helps in reducing the compile
time.
Example:
for(int k=0;k<10;k++)
{
x = k*2;
for(int k=0;k<10;k++)
y = k+3;
for(int k=0;k<10;k++)
x = k*2;
y = k+3;
3. Loop Unrolling:
It helps in optimizing the execution time of the program by reducing the iterations.
It increases the program’s speed by eliminating the loop control and test
instructions.
Example:
printf("Hello");
printf("Hello");
printf("Hello");
Conclusion