SlideShare a Scribd company logo
CODE OPTIMIZATION
PRSENTED BY:
SANJEEV KUMAR……
DEPT:-IT.
ASSAM UNIVERSITY ,SILCHAR
Design Of a Compiler
Lexical Analysis
Syntax Analysis
Intermediate Code Generation
Code Generation
Code Optimization
Table
Mgmt
Routine
Error
Handling
Routine
Source code
Object code
What is optimization?
 In computing, optimization is the process of modifying a system to
make some aspect of it work more efficiently or use fewer resources.
For instance, a computer program may be optimized so that it
executes more rapidly, or is capable of operating with less memory
storage or other resources, or draw less power. The system may be a
single computer program, a collection of computers or even an entire
network such as the internet.
code optimization
Levels' of optimization
Optimization can occur at a number of 'levels':
 Design level
At the highest level, the design may be optimized to make best
use of the available resources. The implementation of this
design will benefit from the use of suitable efficient algorithms
and the implementation of these algorithms will benefit from
writing good quality code. The architectural design of a system
overwhelmingly affects its performance. The choice of
algorithm affects efficiency more than any other item of the
design.
 Compile level
Use of an optimizing compiler tends to ensure that the
executable program is optimized at least as much as the
compiler can predict.
 Assembly level
At the lowest level, writing code using an Assembly language
designed for a particular hardware platform will normally produce
the most efficient code since the programmer can take advantage of
the full repertoire of machine instructions. The operating systems of
most machines has been traditionally written in Assembler code for
this reason.
 Runtime
Just In Time Compiler and assembler programmers are able to
perform runtime optimization.
When to optimize ?
Optimization is often performed at the end of the
development stage since it
• reduces readability
• adds code that is used to improve the performance.
Criteria For optimization
An optimization must preserve the meaning of a
program :
-Cannot change the output produced for any input
-Can not introduce an error
optimization should, on average, speed up
programs
Transformation should be worth the effort
Improvements can be made at various phases:
Source Code:
-Algorithms transformations can produce spectacular improvements
-Profiling can be helpful to focus a programmer’s attention on important
code.
Intermediate Code:
-Compiler can improve loops, procedure calls and address calculations
-Typically only optimizing compilers include this phase
Target Code:
-Compilers can use registers efficiently
-Peephole transformation can be applied
Types of Code optimization
 Common Sub-expression Removal
 Dead Code Optimization
 Loop Optimization
Common Sub expression elimination
Common Sub expression elimination is a optimization that
searches for instances of identical expressions (i.e they all
evaluate the same value), and analyses whether it is
worthwhile replacing with a single variable holding the
computed value.
a=b * c + g
d=b * c * d
temp=b * c
a=temp + g
d=temp * d
Dead Code elimination is a compiler optimization that removes code that
does not affect a program. Removing such code has two benefits It shrinks
program size, an important consideration in some contexts. It lets the running
program avoid executing irrelevant operations, which reduces its running
time.
Dead Code elimination is of two types
Unreachable Code
Redundant statement
Dead code Optimization:
Unreachable Code
In Computer Programming, Unreachable Code or dead code is code that
exists in the source code of a program but can never be executed.
Program Code
If (a>b)
m=a
elseif (a<b)
m=b
elseif (a==b)
m=0
else
m=-1
Optimized Code
If (a>b)
m=a
elseif (a<b)
m=b
else
m=0
Redundant Code
Redundant Code is code that is executed but has
no effect on the output from a program
main(){
int a,b,c,r;
a=5;
b=6;
c=a + b;
r=2;
r++;
printf(“%d”,c);
}
Adding time & space complexity
Loop optimization
Loop optimization plays an important role in improving
the performance of the source code by reducing overheads
associated with executing loops.
Loop Optimization can be done by removing:
• Loop invariant
• Induction variables
Loop Invariant
i = 1
s= 0
do{
s= s + i
a =5
i = i + 1
{
while (i < =n)
i = 1
s= 0
a =5
do{
s= s + i
i = i + 1
{
while (i < =n)
Bringing a=5 outside the do while loop, is called code
motion.
Induction variables
i = 1
s= 0
S1=0
S2=0
while (i < =n)
{
s= s + a[ i ]
t1 = i * 4
s= s + b[ t1 ]
t2 = t1 +2
s2= s2 + c[ t2 ]
i = i + 1
}
i = 1
s= 0
S1=0
S2=0
t2=0
while (i < =n)
{
s= s + a[ i ]
t1 = t1+ 4
s= s + b[ t1 ]
s2= s2 + c[t1 +2 ]
i = i + 1
}
t1,t2 are induction variables. i is inducing t1 and t1 is inducing t2
“+” replaced “ * ”,
t1 was made
independent of i
code optimization
code optimization
code optimization
code optimization
Common Sub-expression Removal
 It is used to remove redundant computations which usually
improves the execution time of a program.
code optimization
code optimization
code optimization
code optimization
code optimization
code optimization
Three Address Code of Quick Sort
i = m - 1i = m - 1
j = nj = n
tt11 =4 * n=4 * n
v = a[tv = a[t11]]
i = i + 1i = i + 1
tt22 = 4 * i= 4 * i
tt33 = a[t= a[t22]]
if tif t33 < v goto (5)< v goto (5)
j = j – 1j = j – 1
tt44 = 4 * j= 4 * j
tt55 = a[t= a[t44]]
if tif t55 > v goto (9)> v goto (9)
if i >= j goto (23)if i >= j goto (23)
tt66 = 4 * i= 4 * i
x = a[tx = a[t66]]
11
22
33
44
55
66
77
88
99
1010
1111
1212
1313
1414
1515
tt77 = 4 * I= 4 * I
tt88 = 4 * j= 4 * j
tt99 = a[t= a[t88]]
a[ta[t77] = t] = t99
tt1010 = 4 * j= 4 * j
a[ta[t1010] = x] = x
goto (5)goto (5)
tt1111 = 4 * I= 4 * I
x = a[tx = a[t1111]]
tt1212 = 4 * i= 4 * i
tt1313 = 4 * n= 4 * n
tt1414 = a[t= a[t1313]]
a[ta[t1212] = t] = t1414
tt1515 = 4 * n= 4 * n
a[ta[t ] = x] = x
1616
1717
1818
1919
2020
2121
2222
2323
2424
2525
2626
2727
2828
2929
3030
Find The Basic Block
i = m - 1i = m - 1
j = nj = n
tt11 =4 * n=4 * n
v = a[tv = a[t11]]
i = i + 1i = i + 1
tt22 = 4 * i= 4 * i
tt33 = a[t= a[t22]]
if tif t33 < v goto (5)< v goto (5)
j = j – 1j = j – 1
tt44 = 4 * j= 4 * j
tt55 = a[t= a[t44]]
if tif t55 > v goto (9)> v goto (9)
if i >= j goto (23)if i >= j goto (23)
tt66 = 4 * i= 4 * i
x = a[tx = a[t66]]
11
22
33
44
55
66
77
88
99
1010
1111
1212
1313
1414
1515
tt77 = 4 * I= 4 * I
tt88 = 4 * j= 4 * j
tt99 = a[t= a[t88]]
a[ta[t77] = t] = t99
tt1010 = 4 * j= 4 * j
a[ta[t1010] = x] = x
goto (5)goto (5)
tt1111 = 4 * i= 4 * i
x = a[tx = a[t1111]]
tt1212 = 4 * i= 4 * i
tt1313 = 4 * n= 4 * n
tt1414 = a[t= a[t1313]]
a[ta[t1212] = t] = t1414
tt1515 = 4 * n= 4 * n
a[ta[t ] = x] = x
1616
1717
1818
1919
2020
2121
2222
2323
2424
2525
2626
2727
2828
2929
3030
Flow Graph
i = m - 1i = m - 1
j = nj = n
tt11 =4 * n=4 * n
v = a[tv = a[t11]]
i = i + 1i = i + 1
tt22 = 4 * i= 4 * i
tt33 = a[t= a[t22]]
if tif t33 < v goto B< v goto B22
j = j – 1j = j – 1
tt44 = 4 * j= 4 * j
tt55 = a[t= a[t44]]
if tif t55 > v goto B> v goto B33
if i >= j goto B6
tt66 = 4 * i= 4 * i
x = a[tx = a[t66]]
tt77 = 4 * i= 4 * i
tt88 = 4 * j= 4 * j
tt99 = a[t= a[t88]]
a[ta[t77] = t] = t99
tt1010 = 4 * j= 4 * j
a[ta[t1010] = x] = x
goto Bgoto B22
tt1111 = 4 * i= 4 * i
x = a[tx = a[t1111]]
tt1212 = 4 * i= 4 * i
tt1313 = 4 * n= 4 * n
tt1414 = a[t= a[t1313]]
a[ta[t1212] = t] = t1414
tt1515 = 4 * n= 4 * n
a[ta[t1515] = x] = x
B1
B2
B3
B4
B5 B6
Common Subexpression Elimination
i = m - 1i = m - 1
j = nj = n
tt11 =4 * n=4 * n
v = a[tv = a[t11]]
i = i + 1i = i + 1
tt22 = 4 * i= 4 * i
tt33 = a[t= a[t22]]
if tif t33 < v goto B< v goto B22
j = j – 1j = j – 1
tt44 = 4 * j= 4 * j
tt55 = a[t= a[t44]]
if tif t55 > v goto B> v goto B33
if i >= j goto B6
tt66 = 4 * i= 4 * i
x = a[tx = a[t66]]
tt77 = 4 * i= 4 * i
tt88 = 4 * j= 4 * j
tt99 = a[t= a[t88]]
a[ta[t77] = t] = t99
tt1010 = 4 * j= 4 * j
a[ta[t1010] = x] = x
goto Bgoto B22
tt1111 = 4 * i= 4 * i
x = a[tx = a[t1111]]
tt1212 = 4 * i= 4 * i
tt1313 = 4 * n= 4 * n
tt1414 = a[t= a[t1313]]
a[ta[t1212] = t] = t1414
tt1515 = 4 * n= 4 * n
a[ta[t1515] = x] = x
B1
B2
B3
B4
B5 B6
Common Subexpression Elimination
i = m - 1i = m - 1
j = nj = n
tt11 =4 * n=4 * n
v = a[tv = a[t11]]
i = i + 1i = i + 1
tt22 = 4 * i= 4 * i
tt33 = a[t= a[t22]]
if tif t33 < v goto B< v goto B22
j = j – 1j = j – 1
tt44 = 4 * j= 4 * j
tt55 = a[t= a[t44]]
if tif t55 > v goto B> v goto B33
if i >= j goto B6
tt66 = 4 * i= 4 * i
x = a[tx = a[t66]]
tt88 = 4 * j= 4 * j
tt99 = a[t= a[t88]]
a[a[tt66] = t] = t99
tt1010 = 4 *= 4 *
jj
a[ta[t1010] = x] = x
goto Bgoto B22
tt1111 = 4 * i= 4 * i
x = a[tx = a[t1111]]
tt1212 = 4 * i= 4 * i
tt1313 = 4 * n= 4 * n
tt1414 = a[t= a[t1313]]
a[ta[t1212] = t] = t1414
tt1515 = 4 * n= 4 * n
a[ta[t1515] = x] = x
B1
B2
B3
B4
B5 B6
Common Subexpression Elimination
i = m - 1i = m - 1
j = nj = n
tt11 =4 * n=4 * n
v = a[tv = a[t11]]
i = i + 1i = i + 1
tt22 = 4 * i= 4 * i
tt33 = a[t= a[t22]]
if tif t33 < v goto B< v goto B22
j = j – 1j = j – 1
tt44 = 4 * j= 4 * j
tt55 = a[t= a[t44]]
if tif t55 > v goto B> v goto B33
if i >= j goto B6
tt66 = 4 * i= 4 * i
x = a[tx = a[t66]]
tt88 = 4 * j= 4 * j
tt99 = a[t= a[t88]]
a[a[tt66] = t] = t99
a[a[tt88] = x] = x
goto Bgoto B22
tt1111 = 4 *i= 4 *i
x = a[tx = a[t1111]]
tt1212 = 4 * i= 4 * i
tt1313 = 4 * n= 4 * n
tt1414 = a[t= a[t1313]]
a[ta[t1212] = t] = t1414
tt1515 = 4 * n= 4 * n
a[ta[t1515] = x] = x
B1
B2
B3
B4
B5 B6
Common Subexpression Elimination
i = m - 1i = m - 1
j = nj = n
tt11 =4 * n=4 * n
v = a[tv = a[t11]]
i = i + 1i = i + 1
tt22 = 4 * i= 4 * i
tt33 = a[t= a[t22]]
if tif t33 < v goto B< v goto B22
j = j – 1j = j – 1
tt44 = 4 * j= 4 * j
tt55 = a[t= a[t44]]
if tif t55 > v goto B> v goto B33
if i >= j goto B6
tt66 = 4 * i= 4 * i
x = a[tx = a[t66]]
tt88 = 4 * j= 4 * j
tt99 = a[t= a[t88]]
a[a[tt66] = t] = t99
a[a[tt88] = x] = x
goto Bgoto B22
tt1111 = 4 * i= 4 * i
x = a[tx = a[t1111]]
tt1212 = 4 * i= 4 * i
tt1313 = 4 * n= 4 * n
tt1414 = a[t= a[t1313]]
a[ta[t1212] = t] = t1414
tt1515 = 4 * n= 4 * n
a[ta[t1515] = x] = x
B1
B2
B3
B4
B5 B6
Common Subexpression Elimination
i = m - 1i = m - 1
j = nj = n
tt11 =4 * n=4 * n
v = a[tv = a[t11]]
i = i + 1i = i + 1
tt22 = 4 * i= 4 * i
tt33 = a[t= a[t22]]
if tif t33 < v goto B< v goto B22
j = j – 1j = j – 1
tt44 = 4 * j= 4 * j
tt55 = a[t= a[t44]]
if tif t55 > v goto B> v goto B33
if i >= j goto B6
tt66 = 4 * i= 4 * i
x = a[tx = a[t66]]
tt88 = 4 * j= 4 * j
tt99 = a[t= a[t88]]
a[a[tt66] = t] = t99
a[a[tt88] = x] = x
goto Bgoto B22
tt1111 = 4 * i= 4 * i
x = a[tx = a[t1111]]
tt1313 = 4 * n= 4 * n
tt1414 = a[t= a[t1313]]
a[a[tt1111] = t] = t1414
tt1515 = 4 * n= 4 * n
a[ta[t1515] = x] = x
B1
B2
B3
B4
B5 B6
Common Subexpression Elimination
i = m - 1i = m - 1
j = nj = n
tt11 =4 * n=4 * n
v = a[tv = a[t11]]
i = i + 1i = i + 1
tt22 = 4 * i= 4 * i
tt33 = a[t= a[t22]]
if tif t33 < v goto B< v goto B22
j = j – 1j = j – 1
tt44 = 4 * j= 4 * j
tt55 = a[t= a[t44]]
if tif t55 > v goto B> v goto B33
if i >= j goto B6
tt66 = 4 * i= 4 * i
x = a[tx = a[t66]]
tt88 = 4 * j= 4 * j
tt99 = a[t= a[t88]]
a[a[tt66] = t] = t99
a[a[tt88] = x] = x
goto Bgoto B22
tt1111 = 4 * i= 4 * i
x = a[tx = a[t1111]]
tt1313 = 4 * n= 4 * n
tt1414 = a[t= a[t1313]]
a[a[tt1111] = t] = t1414
a[a[tt1313] = x] = x
B1
B2
B3
B4
B5 B6
Common Subexpression Elimination
i = m - 1i = m - 1
j = nj = n
tt11 =4 * n=4 * n
v = a[tv = a[t11]]
i = i + 1i = i + 1
tt22 = 4 * i= 4 * i
tt33 = a[t= a[t22]]
if tif t33 < v goto B< v goto B22
j = j – 1j = j – 1
tt44 = 4 * j= 4 * j
tt55 = a[t= a[t44]]
if tif t55 > v goto B> v goto B33
if i >= j goto B6
tt66 = 4 * i= 4 * i
x = a[tx = a[t66]]
tt88 = 4 * j= 4 * j
tt99 = a[t= a[t88]]
a[a[tt66] = t] = t99
a[a[tt88] = x] = x
goto Bgoto B22
tt1111 = 4 * i= 4 * i
x = a[tx = a[t1111]]
tt1313 = 4 * n= 4 * n
tt1414 = a[t= a[t1313]]
a[a[tt1111] = t] = t1414
a[a[tt1313] = x] = x
B1
B2
B3
B4
B5 B6
Common Subexpression Elimination
i = m - 1i = m - 1
j = nj = n
tt11 =4 * n=4 * n
v = a[tv = a[t11]]
i = i + 1i = i + 1
tt22 = 4 * i= 4 * i
tt33 = a[t= a[t22]]
if tif t33 < v goto B< v goto B22
j = j – 1j = j – 1
tt44 = 4 * j= 4 * j
tt55 = a[t= a[t44]]
if tif t55 > v goto B> v goto B33
if i >= j goto B6
x = a[x = a[tt22]]
tt88 = 4 * j= 4 * j
tt99 = a[t= a[t88]]
a[a[tt22] = t] = t99
a[a[tt88] = x] = x
goto Bgoto B22
tt1111 = 4 * i= 4 * i
x = a[tx = a[t1111]]
tt1313 = 4 * n= 4 * n
tt1414 = a[t= a[t1313]]
a[a[tt1111] = t] = t1414
a[a[tt1313] = x] = x
B1
B2
B3
B4
B5 B6
Common Subexpression Elimination
i = m - 1i = m - 1
j = nj = n
tt11 =4 * n=4 * n
v = a[tv = a[t11]]
i = i + 1i = i + 1
tt22 = 4 * i= 4 * i
tt33 = a[t= a[t22]]
if tif t33 < v goto B< v goto B22
j = j – 1j = j – 1
tt44 = 4 * j= 4 * j
tt55 = a[t= a[t44]]
if tif t55 > v goto B> v goto B33
if i >= j goto B6
x = tx = t33
tt88 = 4 * j= 4 * j
tt99 = a[t= a[t88]]
a[a[tt22] = t] = t99
a[a[tt88] = x] = x
goto Bgoto B22
tt1111 = 4 * i= 4 * i
x = a[tx = a[t1111]]
tt1313 = 4 * n= 4 * n
tt1414 = a[t= a[t1313]]
a[a[tt1111] = t] = t1414
a[a[tt1313] = x] = x
B1
B2
B3
B4
B5 B6
Common Subexpression Elimination
i = m - 1i = m - 1
j = nj = n
tt11 =4 * n=4 * n
v = a[tv = a[t11]]
i = i + 1i = i + 1
tt22 = 4 * i= 4 * i
tt33 = a[t= a[t22]]
if tif t33 < v goto B< v goto B22
j = j – 1j = j – 1
tt44 = 4 * j= 4 * j
tt55 = a[t= a[t44]]
if tif t55 > v goto B> v goto B33
if i >= j goto B6
x = tx = t33
a[a[tt22] = t] = t55
a[a[tt44] = x] = x
goto Bgoto B22
tt1111 = 4 * i= 4 * i
x = a[tx = a[t1111]]
tt1313 = 4 * n= 4 * n
tt1414 = a[t= a[t1313]]
a[a[tt1111] = t] = t1414
a[a[tt1313] = x] = x
B1
B2
B3
B4
B5 B6
Common Subexpression Elimination
i = m - 1i = m - 1
j = nj = n
tt11 =4 * n=4 * n
v = a[tv = a[t11]]
i = i + 1i = i + 1
tt22 = 4 * i= 4 * i
tt33 = a[t= a[t22]]
if tif t33 < v goto B< v goto B22
j = j – 1j = j – 1
tt44 = 4 * j= 4 * j
tt55 = a[t= a[t44]]
if tif t55 > v goto B> v goto B33
if i >= j goto B6
x = tx = t33
a[a[tt22] = t] = t55
a[a[tt44] = x] = x
goto Bgoto B22
x = tx = t33
tt1414 = a[t= a[t11]]
a[a[tt22] = t] = t1414
a[a[tt11] = x] = x
B1
B2
B3
B4
B5 B6
Similarly for B6
Dead Code Elimination
i = m - 1i = m - 1
j = nj = n
tt11 =4 * n=4 * n
v = a[tv = a[t11]]
i = i + 1i = i + 1
tt22 = 4 * i= 4 * i
tt33 = a[t= a[t22]]
if tif t33 < v goto B< v goto B22
j = j – 1j = j – 1
tt44 = 4 * j= 4 * j
tt55 = a[t= a[t44]]
if tif t55 > v goto B> v goto B33
if i >= j goto B6
x = tx = t33
a[a[tt22] = t] = t55
a[a[tt44] = x] = x
goto Bgoto B22
x = tx = t33
tt1414 = a[t= a[t11]]
a[a[tt22] = t] = t1414
a[a[tt11] = x] = x
B1
B2
B3
B4
B5 B6
Dead Code Elimination
i = m - 1i = m - 1
j = nj = n
tt11 =4 * n=4 * n
v = a[tv = a[t11]]
i = i + 1i = i + 1
tt22 = 4 * i= 4 * i
tt33 = a[t= a[t22]]
if tif t33 < v goto B< v goto B22
j = j – 1j = j – 1
tt44 = 4 * j= 4 * j
tt55 = a[t= a[t44]]
if tif t55 > v goto B> v goto B33
if i >= j goto B6
a[a[tt22] = t] = t55
a[a[tt44] = t] = t33
goto Bgoto B22
tt1414 = a[t= a[t11]]
a[a[tt22] = t] = t1414
a[a[tt11] = t] = t33
B1
B2
B3
B4
B5 B6
Reduction in Strength
i = m - 1i = m - 1
j = nj = n
tt11 =4 * n=4 * n
v = a[tv = a[t11]]
i = i + 1i = i + 1
tt22 = 4 * i= 4 * i
tt33 = a[t= a[t22]]
if tif t33 < v goto B< v goto B22
j = j – 1j = j – 1
tt44 = 4 * j= 4 * j
tt55 = a[t= a[t44]]
if tif t55 > v goto B> v goto B33
if i >= j goto B6
a[a[tt22] = t] = t55
a[a[tt44] = t] = t33
goto Bgoto B22
tt1414 = a[t= a[t11]]
a[a[tt22] = t] = t1414
a[a[tt11] = t] = t33
B1
B2
B3
B4
B5 B6
Reduction in Strength
i = m - 1i = m - 1
j = nj = n
tt11 =4 * n=4 * n
v = a[tv = a[t11]]
tt22 = 4 * i= 4 * i
tt44 = 4 * j= 4 * j
tt22 = t= t22 + 4+ 4
tt33 = a[t= a[t22]]
if tif t33 < v goto B< v goto B22
tt44 = t= t44 - 4- 4
tt55 = a[t= a[t44]]
if tif t55 > v goto B> v goto B33
if i >= j goto B6
a[a[tt22] = t] = t55
a[a[tt44] = t] = t33
goto Bgoto B22
tt1414 = a[t= a[t11]]
a[a[tt22] = t] = t1414
a[a[tt11] = t] = t33
B1
B2
B3
B4
B5 B6

More Related Content

PPT
basics of compiler design
PPT
Code Optimization
PPTX
Language Translator ( Compiler)
PPTX
Ch 3 Assembler in System programming
PPT
Chapter One
PPT
Microinstruction sequencing new
PPTX
Code optimization
PDF
5.Elements of Assembly Language in System Software.pdf
basics of compiler design
Code Optimization
Language Translator ( Compiler)
Ch 3 Assembler in System programming
Chapter One
Microinstruction sequencing new
Code optimization
5.Elements of Assembly Language in System Software.pdf

What's hot (20)

PPTX
Text Editor for System Software
PPTX
Compiler construction tools
PPTX
Toy compiler
PPT
Compiler Design
PPTX
Phases of Compiler
PPTX
System Programming- Unit I
PPTX
Unit 3 sp assembler
PPSX
Spr ch-02
PPT
Loop invariant computation
PPTX
Compiler vs interpreter
PPT
Compiler Design Basics
PPTX
System Programming Overview
PPTX
Use case point ( Software Estimation Technique)
PPTX
Memory management in operating system | Paging | Virtual memory
PPT
1.Role lexical Analyzer
PDF
Compiler Construction | Lecture 1 | What is a compiler?
PPTX
Ch 4 linker loader
PPTX
Toy complier
PPTX
Basic Blocks and Flow Graphs
Text Editor for System Software
Compiler construction tools
Toy compiler
Compiler Design
Phases of Compiler
System Programming- Unit I
Unit 3 sp assembler
Spr ch-02
Loop invariant computation
Compiler vs interpreter
Compiler Design Basics
System Programming Overview
Use case point ( Software Estimation Technique)
Memory management in operating system | Paging | Virtual memory
1.Role lexical Analyzer
Compiler Construction | Lecture 1 | What is a compiler?
Ch 4 linker loader
Toy complier
Basic Blocks and Flow Graphs
Ad

Similar to code optimization (20)

PPT
457418.-Compiler-Design-Code-optimization.ppt
PPT
code optimization
PPTX
Code optmize.pptx which is related to coding
PPT
basics of optimizations presentation s
PDF
PDF
PPTX
Principle source of optimazation
PPTX
Introduction to code optimization by dipankar
PPTX
complier design unit 5 for helping students
PPT
lect23_optimization.ppt
PPTX
Machine_Learning_JNTUH_R18_UNIT5_CONCEPTS.pptx
PPT
Code Optimization Lec#7.ppt Code Optimizer
PPTX
Project management
PPTX
Compiler Design_Code Optimization tech.pptx
PDF
Numerical Solution of Linear algebraic Equation
PPT
Code optimisation presnted
PPTX
Principal Sources of Optimization in compiler design
PPT
DynamicProgramming.ppt
PDF
Dynamic programming
PPTX
Principal source of optimization in compiler design
457418.-Compiler-Design-Code-optimization.ppt
code optimization
Code optmize.pptx which is related to coding
basics of optimizations presentation s
Principle source of optimazation
Introduction to code optimization by dipankar
complier design unit 5 for helping students
lect23_optimization.ppt
Machine_Learning_JNTUH_R18_UNIT5_CONCEPTS.pptx
Code Optimization Lec#7.ppt Code Optimizer
Project management
Compiler Design_Code Optimization tech.pptx
Numerical Solution of Linear algebraic Equation
Code optimisation presnted
Principal Sources of Optimization in compiler design
DynamicProgramming.ppt
Dynamic programming
Principal source of optimization in compiler design
Ad

Recently uploaded (20)

PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Insiders guide to clinical Medicine.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
TR - Agricultural Crops Production NC III.pdf
PPTX
Lesson notes of climatology university.
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
RMMM.pdf make it easy to upload and study
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
master seminar digital applications in india
PPTX
Cell Types and Its function , kingdom of life
PDF
Sports Quiz easy sports quiz sports quiz
PPTX
Cell Structure & Organelles in detailed.
O5-L3 Freight Transport Ops (International) V1.pdf
Insiders guide to clinical Medicine.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Anesthesia in Laparoscopic Surgery in India
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Microbial diseases, their pathogenesis and prophylaxis
O7-L3 Supply Chain Operations - ICLT Program
TR - Agricultural Crops Production NC III.pdf
Lesson notes of climatology university.
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
RMMM.pdf make it easy to upload and study
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Abdominal Access Techniques with Prof. Dr. R K Mishra
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
STATICS OF THE RIGID BODIES Hibbelers.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
master seminar digital applications in india
Cell Types and Its function , kingdom of life
Sports Quiz easy sports quiz sports quiz
Cell Structure & Organelles in detailed.

code optimization

  • 1. CODE OPTIMIZATION PRSENTED BY: SANJEEV KUMAR…… DEPT:-IT. ASSAM UNIVERSITY ,SILCHAR
  • 2. Design Of a Compiler Lexical Analysis Syntax Analysis Intermediate Code Generation Code Generation Code Optimization Table Mgmt Routine Error Handling Routine Source code Object code
  • 3. What is optimization?  In computing, optimization is the process of modifying a system to make some aspect of it work more efficiently or use fewer resources. For instance, a computer program may be optimized so that it executes more rapidly, or is capable of operating with less memory storage or other resources, or draw less power. The system may be a single computer program, a collection of computers or even an entire network such as the internet.
  • 5. Levels' of optimization Optimization can occur at a number of 'levels':  Design level At the highest level, the design may be optimized to make best use of the available resources. The implementation of this design will benefit from the use of suitable efficient algorithms and the implementation of these algorithms will benefit from writing good quality code. The architectural design of a system overwhelmingly affects its performance. The choice of algorithm affects efficiency more than any other item of the design.  Compile level Use of an optimizing compiler tends to ensure that the executable program is optimized at least as much as the compiler can predict.
  • 6.  Assembly level At the lowest level, writing code using an Assembly language designed for a particular hardware platform will normally produce the most efficient code since the programmer can take advantage of the full repertoire of machine instructions. The operating systems of most machines has been traditionally written in Assembler code for this reason.  Runtime Just In Time Compiler and assembler programmers are able to perform runtime optimization.
  • 7. When to optimize ? Optimization is often performed at the end of the development stage since it • reduces readability • adds code that is used to improve the performance.
  • 8. Criteria For optimization An optimization must preserve the meaning of a program : -Cannot change the output produced for any input -Can not introduce an error optimization should, on average, speed up programs Transformation should be worth the effort
  • 9. Improvements can be made at various phases: Source Code: -Algorithms transformations can produce spectacular improvements -Profiling can be helpful to focus a programmer’s attention on important code. Intermediate Code: -Compiler can improve loops, procedure calls and address calculations -Typically only optimizing compilers include this phase Target Code: -Compilers can use registers efficiently -Peephole transformation can be applied
  • 10. Types of Code optimization  Common Sub-expression Removal  Dead Code Optimization  Loop Optimization
  • 11. Common Sub expression elimination Common Sub expression elimination is a optimization that searches for instances of identical expressions (i.e they all evaluate the same value), and analyses whether it is worthwhile replacing with a single variable holding the computed value. a=b * c + g d=b * c * d temp=b * c a=temp + g d=temp * d
  • 12. Dead Code elimination is a compiler optimization that removes code that does not affect a program. Removing such code has two benefits It shrinks program size, an important consideration in some contexts. It lets the running program avoid executing irrelevant operations, which reduces its running time. Dead Code elimination is of two types Unreachable Code Redundant statement Dead code Optimization:
  • 13. Unreachable Code In Computer Programming, Unreachable Code or dead code is code that exists in the source code of a program but can never be executed. Program Code If (a>b) m=a elseif (a<b) m=b elseif (a==b) m=0 else m=-1 Optimized Code If (a>b) m=a elseif (a<b) m=b else m=0
  • 14. Redundant Code Redundant Code is code that is executed but has no effect on the output from a program main(){ int a,b,c,r; a=5; b=6; c=a + b; r=2; r++; printf(“%d”,c); } Adding time & space complexity
  • 15. Loop optimization Loop optimization plays an important role in improving the performance of the source code by reducing overheads associated with executing loops. Loop Optimization can be done by removing: • Loop invariant • Induction variables
  • 16. Loop Invariant i = 1 s= 0 do{ s= s + i a =5 i = i + 1 { while (i < =n) i = 1 s= 0 a =5 do{ s= s + i i = i + 1 { while (i < =n) Bringing a=5 outside the do while loop, is called code motion.
  • 17. Induction variables i = 1 s= 0 S1=0 S2=0 while (i < =n) { s= s + a[ i ] t1 = i * 4 s= s + b[ t1 ] t2 = t1 +2 s2= s2 + c[ t2 ] i = i + 1 } i = 1 s= 0 S1=0 S2=0 t2=0 while (i < =n) { s= s + a[ i ] t1 = t1+ 4 s= s + b[ t1 ] s2= s2 + c[t1 +2 ] i = i + 1 } t1,t2 are induction variables. i is inducing t1 and t1 is inducing t2 “+” replaced “ * ”, t1 was made independent of i
  • 22. Common Sub-expression Removal  It is used to remove redundant computations which usually improves the execution time of a program.
  • 29. Three Address Code of Quick Sort i = m - 1i = m - 1 j = nj = n tt11 =4 * n=4 * n v = a[tv = a[t11]] i = i + 1i = i + 1 tt22 = 4 * i= 4 * i tt33 = a[t= a[t22]] if tif t33 < v goto (5)< v goto (5) j = j – 1j = j – 1 tt44 = 4 * j= 4 * j tt55 = a[t= a[t44]] if tif t55 > v goto (9)> v goto (9) if i >= j goto (23)if i >= j goto (23) tt66 = 4 * i= 4 * i x = a[tx = a[t66]] 11 22 33 44 55 66 77 88 99 1010 1111 1212 1313 1414 1515 tt77 = 4 * I= 4 * I tt88 = 4 * j= 4 * j tt99 = a[t= a[t88]] a[ta[t77] = t] = t99 tt1010 = 4 * j= 4 * j a[ta[t1010] = x] = x goto (5)goto (5) tt1111 = 4 * I= 4 * I x = a[tx = a[t1111]] tt1212 = 4 * i= 4 * i tt1313 = 4 * n= 4 * n tt1414 = a[t= a[t1313]] a[ta[t1212] = t] = t1414 tt1515 = 4 * n= 4 * n a[ta[t ] = x] = x 1616 1717 1818 1919 2020 2121 2222 2323 2424 2525 2626 2727 2828 2929 3030
  • 30. Find The Basic Block i = m - 1i = m - 1 j = nj = n tt11 =4 * n=4 * n v = a[tv = a[t11]] i = i + 1i = i + 1 tt22 = 4 * i= 4 * i tt33 = a[t= a[t22]] if tif t33 < v goto (5)< v goto (5) j = j – 1j = j – 1 tt44 = 4 * j= 4 * j tt55 = a[t= a[t44]] if tif t55 > v goto (9)> v goto (9) if i >= j goto (23)if i >= j goto (23) tt66 = 4 * i= 4 * i x = a[tx = a[t66]] 11 22 33 44 55 66 77 88 99 1010 1111 1212 1313 1414 1515 tt77 = 4 * I= 4 * I tt88 = 4 * j= 4 * j tt99 = a[t= a[t88]] a[ta[t77] = t] = t99 tt1010 = 4 * j= 4 * j a[ta[t1010] = x] = x goto (5)goto (5) tt1111 = 4 * i= 4 * i x = a[tx = a[t1111]] tt1212 = 4 * i= 4 * i tt1313 = 4 * n= 4 * n tt1414 = a[t= a[t1313]] a[ta[t1212] = t] = t1414 tt1515 = 4 * n= 4 * n a[ta[t ] = x] = x 1616 1717 1818 1919 2020 2121 2222 2323 2424 2525 2626 2727 2828 2929 3030
  • 31. Flow Graph i = m - 1i = m - 1 j = nj = n tt11 =4 * n=4 * n v = a[tv = a[t11]] i = i + 1i = i + 1 tt22 = 4 * i= 4 * i tt33 = a[t= a[t22]] if tif t33 < v goto B< v goto B22 j = j – 1j = j – 1 tt44 = 4 * j= 4 * j tt55 = a[t= a[t44]] if tif t55 > v goto B> v goto B33 if i >= j goto B6 tt66 = 4 * i= 4 * i x = a[tx = a[t66]] tt77 = 4 * i= 4 * i tt88 = 4 * j= 4 * j tt99 = a[t= a[t88]] a[ta[t77] = t] = t99 tt1010 = 4 * j= 4 * j a[ta[t1010] = x] = x goto Bgoto B22 tt1111 = 4 * i= 4 * i x = a[tx = a[t1111]] tt1212 = 4 * i= 4 * i tt1313 = 4 * n= 4 * n tt1414 = a[t= a[t1313]] a[ta[t1212] = t] = t1414 tt1515 = 4 * n= 4 * n a[ta[t1515] = x] = x B1 B2 B3 B4 B5 B6
  • 32. Common Subexpression Elimination i = m - 1i = m - 1 j = nj = n tt11 =4 * n=4 * n v = a[tv = a[t11]] i = i + 1i = i + 1 tt22 = 4 * i= 4 * i tt33 = a[t= a[t22]] if tif t33 < v goto B< v goto B22 j = j – 1j = j – 1 tt44 = 4 * j= 4 * j tt55 = a[t= a[t44]] if tif t55 > v goto B> v goto B33 if i >= j goto B6 tt66 = 4 * i= 4 * i x = a[tx = a[t66]] tt77 = 4 * i= 4 * i tt88 = 4 * j= 4 * j tt99 = a[t= a[t88]] a[ta[t77] = t] = t99 tt1010 = 4 * j= 4 * j a[ta[t1010] = x] = x goto Bgoto B22 tt1111 = 4 * i= 4 * i x = a[tx = a[t1111]] tt1212 = 4 * i= 4 * i tt1313 = 4 * n= 4 * n tt1414 = a[t= a[t1313]] a[ta[t1212] = t] = t1414 tt1515 = 4 * n= 4 * n a[ta[t1515] = x] = x B1 B2 B3 B4 B5 B6
  • 33. Common Subexpression Elimination i = m - 1i = m - 1 j = nj = n tt11 =4 * n=4 * n v = a[tv = a[t11]] i = i + 1i = i + 1 tt22 = 4 * i= 4 * i tt33 = a[t= a[t22]] if tif t33 < v goto B< v goto B22 j = j – 1j = j – 1 tt44 = 4 * j= 4 * j tt55 = a[t= a[t44]] if tif t55 > v goto B> v goto B33 if i >= j goto B6 tt66 = 4 * i= 4 * i x = a[tx = a[t66]] tt88 = 4 * j= 4 * j tt99 = a[t= a[t88]] a[a[tt66] = t] = t99 tt1010 = 4 *= 4 * jj a[ta[t1010] = x] = x goto Bgoto B22 tt1111 = 4 * i= 4 * i x = a[tx = a[t1111]] tt1212 = 4 * i= 4 * i tt1313 = 4 * n= 4 * n tt1414 = a[t= a[t1313]] a[ta[t1212] = t] = t1414 tt1515 = 4 * n= 4 * n a[ta[t1515] = x] = x B1 B2 B3 B4 B5 B6
  • 34. Common Subexpression Elimination i = m - 1i = m - 1 j = nj = n tt11 =4 * n=4 * n v = a[tv = a[t11]] i = i + 1i = i + 1 tt22 = 4 * i= 4 * i tt33 = a[t= a[t22]] if tif t33 < v goto B< v goto B22 j = j – 1j = j – 1 tt44 = 4 * j= 4 * j tt55 = a[t= a[t44]] if tif t55 > v goto B> v goto B33 if i >= j goto B6 tt66 = 4 * i= 4 * i x = a[tx = a[t66]] tt88 = 4 * j= 4 * j tt99 = a[t= a[t88]] a[a[tt66] = t] = t99 a[a[tt88] = x] = x goto Bgoto B22 tt1111 = 4 *i= 4 *i x = a[tx = a[t1111]] tt1212 = 4 * i= 4 * i tt1313 = 4 * n= 4 * n tt1414 = a[t= a[t1313]] a[ta[t1212] = t] = t1414 tt1515 = 4 * n= 4 * n a[ta[t1515] = x] = x B1 B2 B3 B4 B5 B6
  • 35. Common Subexpression Elimination i = m - 1i = m - 1 j = nj = n tt11 =4 * n=4 * n v = a[tv = a[t11]] i = i + 1i = i + 1 tt22 = 4 * i= 4 * i tt33 = a[t= a[t22]] if tif t33 < v goto B< v goto B22 j = j – 1j = j – 1 tt44 = 4 * j= 4 * j tt55 = a[t= a[t44]] if tif t55 > v goto B> v goto B33 if i >= j goto B6 tt66 = 4 * i= 4 * i x = a[tx = a[t66]] tt88 = 4 * j= 4 * j tt99 = a[t= a[t88]] a[a[tt66] = t] = t99 a[a[tt88] = x] = x goto Bgoto B22 tt1111 = 4 * i= 4 * i x = a[tx = a[t1111]] tt1212 = 4 * i= 4 * i tt1313 = 4 * n= 4 * n tt1414 = a[t= a[t1313]] a[ta[t1212] = t] = t1414 tt1515 = 4 * n= 4 * n a[ta[t1515] = x] = x B1 B2 B3 B4 B5 B6
  • 36. Common Subexpression Elimination i = m - 1i = m - 1 j = nj = n tt11 =4 * n=4 * n v = a[tv = a[t11]] i = i + 1i = i + 1 tt22 = 4 * i= 4 * i tt33 = a[t= a[t22]] if tif t33 < v goto B< v goto B22 j = j – 1j = j – 1 tt44 = 4 * j= 4 * j tt55 = a[t= a[t44]] if tif t55 > v goto B> v goto B33 if i >= j goto B6 tt66 = 4 * i= 4 * i x = a[tx = a[t66]] tt88 = 4 * j= 4 * j tt99 = a[t= a[t88]] a[a[tt66] = t] = t99 a[a[tt88] = x] = x goto Bgoto B22 tt1111 = 4 * i= 4 * i x = a[tx = a[t1111]] tt1313 = 4 * n= 4 * n tt1414 = a[t= a[t1313]] a[a[tt1111] = t] = t1414 tt1515 = 4 * n= 4 * n a[ta[t1515] = x] = x B1 B2 B3 B4 B5 B6
  • 37. Common Subexpression Elimination i = m - 1i = m - 1 j = nj = n tt11 =4 * n=4 * n v = a[tv = a[t11]] i = i + 1i = i + 1 tt22 = 4 * i= 4 * i tt33 = a[t= a[t22]] if tif t33 < v goto B< v goto B22 j = j – 1j = j – 1 tt44 = 4 * j= 4 * j tt55 = a[t= a[t44]] if tif t55 > v goto B> v goto B33 if i >= j goto B6 tt66 = 4 * i= 4 * i x = a[tx = a[t66]] tt88 = 4 * j= 4 * j tt99 = a[t= a[t88]] a[a[tt66] = t] = t99 a[a[tt88] = x] = x goto Bgoto B22 tt1111 = 4 * i= 4 * i x = a[tx = a[t1111]] tt1313 = 4 * n= 4 * n tt1414 = a[t= a[t1313]] a[a[tt1111] = t] = t1414 a[a[tt1313] = x] = x B1 B2 B3 B4 B5 B6
  • 38. Common Subexpression Elimination i = m - 1i = m - 1 j = nj = n tt11 =4 * n=4 * n v = a[tv = a[t11]] i = i + 1i = i + 1 tt22 = 4 * i= 4 * i tt33 = a[t= a[t22]] if tif t33 < v goto B< v goto B22 j = j – 1j = j – 1 tt44 = 4 * j= 4 * j tt55 = a[t= a[t44]] if tif t55 > v goto B> v goto B33 if i >= j goto B6 tt66 = 4 * i= 4 * i x = a[tx = a[t66]] tt88 = 4 * j= 4 * j tt99 = a[t= a[t88]] a[a[tt66] = t] = t99 a[a[tt88] = x] = x goto Bgoto B22 tt1111 = 4 * i= 4 * i x = a[tx = a[t1111]] tt1313 = 4 * n= 4 * n tt1414 = a[t= a[t1313]] a[a[tt1111] = t] = t1414 a[a[tt1313] = x] = x B1 B2 B3 B4 B5 B6
  • 39. Common Subexpression Elimination i = m - 1i = m - 1 j = nj = n tt11 =4 * n=4 * n v = a[tv = a[t11]] i = i + 1i = i + 1 tt22 = 4 * i= 4 * i tt33 = a[t= a[t22]] if tif t33 < v goto B< v goto B22 j = j – 1j = j – 1 tt44 = 4 * j= 4 * j tt55 = a[t= a[t44]] if tif t55 > v goto B> v goto B33 if i >= j goto B6 x = a[x = a[tt22]] tt88 = 4 * j= 4 * j tt99 = a[t= a[t88]] a[a[tt22] = t] = t99 a[a[tt88] = x] = x goto Bgoto B22 tt1111 = 4 * i= 4 * i x = a[tx = a[t1111]] tt1313 = 4 * n= 4 * n tt1414 = a[t= a[t1313]] a[a[tt1111] = t] = t1414 a[a[tt1313] = x] = x B1 B2 B3 B4 B5 B6
  • 40. Common Subexpression Elimination i = m - 1i = m - 1 j = nj = n tt11 =4 * n=4 * n v = a[tv = a[t11]] i = i + 1i = i + 1 tt22 = 4 * i= 4 * i tt33 = a[t= a[t22]] if tif t33 < v goto B< v goto B22 j = j – 1j = j – 1 tt44 = 4 * j= 4 * j tt55 = a[t= a[t44]] if tif t55 > v goto B> v goto B33 if i >= j goto B6 x = tx = t33 tt88 = 4 * j= 4 * j tt99 = a[t= a[t88]] a[a[tt22] = t] = t99 a[a[tt88] = x] = x goto Bgoto B22 tt1111 = 4 * i= 4 * i x = a[tx = a[t1111]] tt1313 = 4 * n= 4 * n tt1414 = a[t= a[t1313]] a[a[tt1111] = t] = t1414 a[a[tt1313] = x] = x B1 B2 B3 B4 B5 B6
  • 41. Common Subexpression Elimination i = m - 1i = m - 1 j = nj = n tt11 =4 * n=4 * n v = a[tv = a[t11]] i = i + 1i = i + 1 tt22 = 4 * i= 4 * i tt33 = a[t= a[t22]] if tif t33 < v goto B< v goto B22 j = j – 1j = j – 1 tt44 = 4 * j= 4 * j tt55 = a[t= a[t44]] if tif t55 > v goto B> v goto B33 if i >= j goto B6 x = tx = t33 a[a[tt22] = t] = t55 a[a[tt44] = x] = x goto Bgoto B22 tt1111 = 4 * i= 4 * i x = a[tx = a[t1111]] tt1313 = 4 * n= 4 * n tt1414 = a[t= a[t1313]] a[a[tt1111] = t] = t1414 a[a[tt1313] = x] = x B1 B2 B3 B4 B5 B6
  • 42. Common Subexpression Elimination i = m - 1i = m - 1 j = nj = n tt11 =4 * n=4 * n v = a[tv = a[t11]] i = i + 1i = i + 1 tt22 = 4 * i= 4 * i tt33 = a[t= a[t22]] if tif t33 < v goto B< v goto B22 j = j – 1j = j – 1 tt44 = 4 * j= 4 * j tt55 = a[t= a[t44]] if tif t55 > v goto B> v goto B33 if i >= j goto B6 x = tx = t33 a[a[tt22] = t] = t55 a[a[tt44] = x] = x goto Bgoto B22 x = tx = t33 tt1414 = a[t= a[t11]] a[a[tt22] = t] = t1414 a[a[tt11] = x] = x B1 B2 B3 B4 B5 B6 Similarly for B6
  • 43. Dead Code Elimination i = m - 1i = m - 1 j = nj = n tt11 =4 * n=4 * n v = a[tv = a[t11]] i = i + 1i = i + 1 tt22 = 4 * i= 4 * i tt33 = a[t= a[t22]] if tif t33 < v goto B< v goto B22 j = j – 1j = j – 1 tt44 = 4 * j= 4 * j tt55 = a[t= a[t44]] if tif t55 > v goto B> v goto B33 if i >= j goto B6 x = tx = t33 a[a[tt22] = t] = t55 a[a[tt44] = x] = x goto Bgoto B22 x = tx = t33 tt1414 = a[t= a[t11]] a[a[tt22] = t] = t1414 a[a[tt11] = x] = x B1 B2 B3 B4 B5 B6
  • 44. Dead Code Elimination i = m - 1i = m - 1 j = nj = n tt11 =4 * n=4 * n v = a[tv = a[t11]] i = i + 1i = i + 1 tt22 = 4 * i= 4 * i tt33 = a[t= a[t22]] if tif t33 < v goto B< v goto B22 j = j – 1j = j – 1 tt44 = 4 * j= 4 * j tt55 = a[t= a[t44]] if tif t55 > v goto B> v goto B33 if i >= j goto B6 a[a[tt22] = t] = t55 a[a[tt44] = t] = t33 goto Bgoto B22 tt1414 = a[t= a[t11]] a[a[tt22] = t] = t1414 a[a[tt11] = t] = t33 B1 B2 B3 B4 B5 B6
  • 45. Reduction in Strength i = m - 1i = m - 1 j = nj = n tt11 =4 * n=4 * n v = a[tv = a[t11]] i = i + 1i = i + 1 tt22 = 4 * i= 4 * i tt33 = a[t= a[t22]] if tif t33 < v goto B< v goto B22 j = j – 1j = j – 1 tt44 = 4 * j= 4 * j tt55 = a[t= a[t44]] if tif t55 > v goto B> v goto B33 if i >= j goto B6 a[a[tt22] = t] = t55 a[a[tt44] = t] = t33 goto Bgoto B22 tt1414 = a[t= a[t11]] a[a[tt22] = t] = t1414 a[a[tt11] = t] = t33 B1 B2 B3 B4 B5 B6
  • 46. Reduction in Strength i = m - 1i = m - 1 j = nj = n tt11 =4 * n=4 * n v = a[tv = a[t11]] tt22 = 4 * i= 4 * i tt44 = 4 * j= 4 * j tt22 = t= t22 + 4+ 4 tt33 = a[t= a[t22]] if tif t33 < v goto B< v goto B22 tt44 = t= t44 - 4- 4 tt55 = a[t= a[t44]] if tif t55 > v goto B> v goto B33 if i >= j goto B6 a[a[tt22] = t] = t55 a[a[tt44] = t] = t33 goto Bgoto B22 tt1414 = a[t= a[t11]] a[a[tt22] = t] = t1414 a[a[tt11] = t] = t33 B1 B2 B3 B4 B5 B6