SlideShare a Scribd company logo
1 of 14
Download to read offline
G H Patel College of Engineering &Technology
Submitted By: -
GandhiVivek
(140110107017)
Gujarat Technological University
SubmittedTo: -
Prof. Namrata Dave
LOOP OPTIMIZATION
Code Optimization: -
• The code optimization can be significantly done in loops of the program.
• Specially inner loop is a place where program spends large amount of time.
• Hence if number of instruction are less in inner loop then the running time of
the program will get decreased to a large extent.
• Hence loop optimization is a technique in which code optimization
performed on inner loops.
Methods of Loop Optimization: -
1. Code Motion
2. InductionVariable and Strength Reduction
3. Loop Invariant Method
4. Loop Unrolling
5. Loop Fusion
Code Motion: -
• Code motion is a technique which moves the code outside the loop.
• If there lies some expression in the loop whose result remain unchanged
even after executing the loop several times, then such an expression should
be placed just before the loop.
Code Motion: -
• For Example: -
while( i <= max - 1 )
{
sum = sum + a[i];
}
• Optimized Code: -
n = max – 1;
while( i <= n )
{
sum = sum + a[i];
}
InductionVariable and Strength Reduction: -
• A variable x is called an induction variable of loop L if the value of variable
gets changed every time.
• It is either decremented or incremented by some constant.
InductionVariable: -
• For Example: -
B1
i := i + 1
t1 := 4*j
t2 := a[t1]
If t2 < 10 goto B1
• In above code the value of i and t1 are in
locked state.
• That is, when value of i gets incremental
by 1 then t1 gets incremented by 4.
• Hence i and t1 are induction variables.
Strength Reduction: -
• The strength of certain operators is higher
then others.
• For Example: -
• Strength of 8 is higher than +.
for ( i = 1; i <= 50; i++)
count = i * 7;
• Here we get values of count as 7,
14 and so on.
temp = 7;
for ( i = 1; i <= 50; i++)
{
count = temp;
temp = temp + 7;
}
Loop Invariant Method: -
• In this optimization technique the computation inside the loop is avoided
and there by the computation overhead on compiler is avoided.
• This ultimately optimizes code generation.
Loop Invariant Method: -
• For Example: -
for i := 0 to 10 do begin
K = i + (a/b);
…
…
end;
• Can be written as
t := a/b;
for i := 0 to 10 do begin
K = i + t;
…
…
end;
Loop Unrolling: -
• For Example: -
int i = 1;
While ( i <=100 )
{
a[i] = b[i];
i++;
}
• Can be written as
int i = 1;
While ( i <=100 )
{
a[i] = b[i];
i++;
a[i] = b[i];
i++;
}
Loop Fusion: -
• For Example: -
for i := 1 to n do
for j := 1 to m do
a[i, j] := 10
• Can be written as: -
for i := 1 to n*m do
a[i] := 10
loopoptimization-180418113642.pdf

More Related Content

Similar to loopoptimization-180418113642.pdf

Similar to loopoptimization-180418113642.pdf (20)

Optimization
OptimizationOptimization
Optimization
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
 
02 performance
02 performance02 performance
02 performance
 
DSA 103 Object Oriented Programming :: Week 3
DSA 103 Object Oriented Programming :: Week 3DSA 103 Object Oriented Programming :: Week 3
DSA 103 Object Oriented Programming :: Week 3
 
sCode optimization
sCode optimizationsCode optimization
sCode optimization
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
 
Algorithms & Complexity Calculation
Algorithms & Complexity CalculationAlgorithms & Complexity Calculation
Algorithms & Complexity Calculation
 
85ec7 session2 c++
85ec7 session2 c++85ec7 session2 c++
85ec7 session2 c++
 
Pipelining slides
Pipelining slides Pipelining slides
Pipelining slides
 
Coa.ppt2
Coa.ppt2Coa.ppt2
Coa.ppt2
 
Begin with c++ Fekra Course #1
Begin with c++ Fekra Course #1Begin with c++ Fekra Course #1
Begin with c++ Fekra Course #1
 
Algorithm analysis and design
Algorithm analysis and designAlgorithm analysis and design
Algorithm analysis and design
 
Searching Algorithms
Searching AlgorithmsSearching Algorithms
Searching Algorithms
 
Cpu performance matrix
Cpu performance matrixCpu performance matrix
Cpu performance matrix
 
Software development best practices & coding guidelines
Software development best practices & coding guidelinesSoftware development best practices & coding guidelines
Software development best practices & coding guidelines
 
Algorithm and C code related to data structure
Algorithm and C code related to data structureAlgorithm and C code related to data structure
Algorithm and C code related to data structure
 
Unit 1, ADA.pptx
Unit 1, ADA.pptxUnit 1, ADA.pptx
Unit 1, ADA.pptx
 
2. Introduction to Algorithm.pptx
2. Introduction to Algorithm.pptx2. Introduction to Algorithm.pptx
2. Introduction to Algorithm.pptx
 
DSA
DSADSA
DSA
 
Compiler optimization techniques
Compiler optimization techniquesCompiler optimization techniques
Compiler optimization techniques
 

More from 16115yogendraSingh

A bug reconnaissance tool is typically software or a system used by cybersecu...
A bug reconnaissance tool is typically software or a system used by cybersecu...A bug reconnaissance tool is typically software or a system used by cybersecu...
A bug reconnaissance tool is typically software or a system used by cybersecu...
16115yogendraSingh
 

More from 16115yogendraSingh (7)

A bug reconnaissance tool is typically software or a system used by cybersecu...
A bug reconnaissance tool is typically software or a system used by cybersecu...A bug reconnaissance tool is typically software or a system used by cybersecu...
A bug reconnaissance tool is typically software or a system used by cybersecu...
 
Presentation on Pandas in _ detail .pptx
Presentation on Pandas in _ detail .pptxPresentation on Pandas in _ detail .pptx
Presentation on Pandas in _ detail .pptx
 
multipleaccesstechniques-201110153309.pptx
multipleaccesstechniques-201110153309.pptxmultipleaccesstechniques-201110153309.pptx
multipleaccesstechniques-201110153309.pptx
 
Python Basics.pptx
Python Basics.pptxPython Basics.pptx
Python Basics.pptx
 
pptseminar.pptx
pptseminar.pptxpptseminar.pptx
pptseminar.pptx
 
51095137-Semantic-WEB.ppt
51095137-Semantic-WEB.ppt51095137-Semantic-WEB.ppt
51095137-Semantic-WEB.ppt
 
Chapter 8 Deriving and Reclassifying Fields.pptx
Chapter 8 Deriving and Reclassifying Fields.pptxChapter 8 Deriving and Reclassifying Fields.pptx
Chapter 8 Deriving and Reclassifying Fields.pptx
 

Recently uploaded

FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
dollysharma2066
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Christo Ananth
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
ankushspencer015
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Dr.Costas Sachpazis
 

Recently uploaded (20)

Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 

loopoptimization-180418113642.pdf

  • 1. G H Patel College of Engineering &Technology Submitted By: - GandhiVivek (140110107017) Gujarat Technological University SubmittedTo: - Prof. Namrata Dave
  • 3. Code Optimization: - • The code optimization can be significantly done in loops of the program. • Specially inner loop is a place where program spends large amount of time. • Hence if number of instruction are less in inner loop then the running time of the program will get decreased to a large extent. • Hence loop optimization is a technique in which code optimization performed on inner loops.
  • 4. Methods of Loop Optimization: - 1. Code Motion 2. InductionVariable and Strength Reduction 3. Loop Invariant Method 4. Loop Unrolling 5. Loop Fusion
  • 5. Code Motion: - • Code motion is a technique which moves the code outside the loop. • If there lies some expression in the loop whose result remain unchanged even after executing the loop several times, then such an expression should be placed just before the loop.
  • 6. Code Motion: - • For Example: - while( i <= max - 1 ) { sum = sum + a[i]; } • Optimized Code: - n = max – 1; while( i <= n ) { sum = sum + a[i]; }
  • 7. InductionVariable and Strength Reduction: - • A variable x is called an induction variable of loop L if the value of variable gets changed every time. • It is either decremented or incremented by some constant.
  • 8. InductionVariable: - • For Example: - B1 i := i + 1 t1 := 4*j t2 := a[t1] If t2 < 10 goto B1 • In above code the value of i and t1 are in locked state. • That is, when value of i gets incremental by 1 then t1 gets incremented by 4. • Hence i and t1 are induction variables.
  • 9. Strength Reduction: - • The strength of certain operators is higher then others. • For Example: - • Strength of 8 is higher than +. for ( i = 1; i <= 50; i++) count = i * 7; • Here we get values of count as 7, 14 and so on. temp = 7; for ( i = 1; i <= 50; i++) { count = temp; temp = temp + 7; }
  • 10. Loop Invariant Method: - • In this optimization technique the computation inside the loop is avoided and there by the computation overhead on compiler is avoided. • This ultimately optimizes code generation.
  • 11. Loop Invariant Method: - • For Example: - for i := 0 to 10 do begin K = i + (a/b); … … end; • Can be written as t := a/b; for i := 0 to 10 do begin K = i + t; … … end;
  • 12. Loop Unrolling: - • For Example: - int i = 1; While ( i <=100 ) { a[i] = b[i]; i++; } • Can be written as int i = 1; While ( i <=100 ) { a[i] = b[i]; i++; a[i] = b[i]; i++; }
  • 13. Loop Fusion: - • For Example: - for i := 1 to n do for j := 1 to m do a[i, j] := 10 • Can be written as: - for i := 1 to n*m do a[i] := 10