Loop Optimization
íncregse execution speed,and Yeduce tHe
to
Loop optimiza tion is q technigue Cache performan ce
tien not o ny mprove
overhead due to loops lhop optmiza
use of pavallel processlng
butit also make effechive
is a machine independeot
optimization
e:- Loop optmlzaton"
execution Hme speot
on the loops
Maximum portion of tode
instuctions in the
inner loop of ven
decrease the number of
When we
then we can Ymprove the running time
the Loop Optimi2athion Techníques
Following are
decrease the code ?nside the lcop.
Motion C Frequency Reduction):
Jn this, we
Nhen
1) Code expession
ouade of loop.
statement or
a
uoe can move in mind
Inthis we need to keep
expression
ou of loop,The
&tatement ov
o e move
or the Semantfc ethe proram,
8heuld et be affecte
hat orjginal oorking
After Cophimlzed)
Example 1 Before
-1
while (i(= ALvalue-1) h ma Malue
ohlle Cn )
Som+z C«j
8 um Sum Smt ZiJ,
Befere AFter opimized)
Example 2:
Z tan()
uohfle CPo) w hileP1o)
8 tanc) + P; Z+P
P P+13
3
2)LooP Unkollin81 Itisa loop ophimíze
transformaton technlque that heips
he exeutm time of the psoqram. Wbat
we ackuall does
'S
We reduce the tevatons. If Unrolling h a n wecan
we perfovm loop:
ng
make psoaram execuhou speed fastes by elimingting loop contol/fesF nste
Before Ophimized Code
E xample
Pint f C" Pankgj \w")%
for Cint i0; i46;it Printf (" Pankaj p")
priotf C"Paokaj\")5 Priotf C" farkaj\n")%
Priotf C Pankaj )
Printf CePankaj \7")
Priotf ( fanaj n"),
pointofi
Those vanables cohose value gotchange at every
3. Incduction Vanable: vanable
can be called as
the Induchon
8uch variabl
durhm optimizes
efere Vaabe2-
exaple- 1): i i+i>valuechato E2=4*i
Nofe value cha33.
E2 4* E2=2+4
and F= V
E aLE2]
Londition Checkic 3F E3 4F then go to Bock B 3F E3F ae to Black B
Sp1Medifrd)
Ep
Optimizecd
Exawple: 21 Before E 43
E4E44
E44 *
E CE43 goto 8lock B3.
IF Es AF
9F Es F got Bloc 83 Exp 2Medif'ed )
Exp2:
use
In orderto
molke code optimized uwe
(4) Reduthon în Stregth:
Caled a Teduuioy in stmeypth
is
and exp2 CModified
we can See this expl Cmadiked)
mulhiplicawon o u t « maio
Yeduce the strength. that is
by moving addihian
How we
tode left and onl
Now ony 3 lines of
bloc, n tompukhey
acdediton teke. les time
fion nee d to perform, (As
pera than mulKpli' cah m)
Redkuechbn Strerdh memod, cwe ophmized the code "
Soui
Befre OpHmied
forCj-1 j<eo; j++) temp T
for Cj-3 ji(20,j+*)
-
calcolate temP
Calwlate *9 temp temP+95