Types of Code Optimization
The code optimization process is mainly classified into two different types:
- Machine Independent Optimization – This code optimization phase attempts to improve the intermediate code to get a better target code as the output. The part of the intermediate code which is transformed here does not involve any CPU registers or absolute memory locations.
- Machine Dependent Optimization – Machine-dependent optimization is done after the target code has been generated and when the code is transformed according to the target machine architecture. It involves CPU registers and may have absolute memory references rather than relative references. Machine-dependent optimizers put efforts to take maximum advantage of the memory hierarchy.
Code Optimization is done in the following different ways
- Compile Time Evaluation :
(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
.
- Variable Propagation :/Before Optimization
c = a * b
x = atill
d = x * b + 4
/After Optimization
c = a * b
x = a
till
d = a * b + 4Hence, after variable propagation, a*b and x*b will be identified as common sub-expression. - Dead code elimination : Variable propagation often leads to making assignment statement into dead codec = a * bx = a
till
d = a * b + 4
/After elimination :
c = a * b
till
d = a * b + 4 - Code Motion :
- Reduce the evaluation frequency of expression.
- Bring loop invariant statements out of the loop.
a = 200;
while(a>0)
{
b = x + y;
if (a % b == 0}
printf(“%d”, a);
}
/This code can be further optimized as
a = 200;
b = x + y;
while(a>0)
{
if (a % b == 0}
printf(“%d”, a);
}
- Induction Variable and Strength Reduction :
- An induction variable is used in the loop for the following kind of assignment i = i + constant.
- Strength reduction means replacing the high strength operator with the low strength.
i = 1;
while (i<10)
{
y = i * 4;
}
/After Reduction
i = 1
t = 4
{
while( t<40)
y = t;
t = t + 4;
}
So this was about code optimization.
You can go with the detailed champion study plan for GATE CS 2021 from the following link:
Detailed GATE CSE 2021 Champion Study Plan
Candidates can also practice 110+ Mock tests for exams like GATE, ISRO, DRDO, BARC, NIELIT, etc. with the BYJU'S Exam Prep Test Series check the following link:
Click Here to Avail GATE CSE Test Series! (100+ Mock Tests)
Get unlimited access to 21+ structured Live Courses, all 112+ mock tests with the Online Classroom Program for GATE CS & PSU Exams:
Click here to avail Online Classroom Program for Computer Science Engineering
Thanks
#DreamStriveSucceed
Comments
write a commentAbhishek DebSep 25, 2018
Lohith NegalurSep 25, 2018
Shirfil 333Sep 28, 2018
Monu KumarSep 29, 2018
Nikhil KumarNov 3, 2019
PraveenOct 22, 2020