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

Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithmsiqbalphy1
 
Algorithms & Complexity Calculation
Algorithms & Complexity CalculationAlgorithms & Complexity Calculation
Algorithms & Complexity CalculationAkhil Kaushik
 
Begin with c++ Fekra Course #1
Begin with c++ Fekra Course #1Begin with c++ Fekra Course #1
Begin with c++ Fekra Course #1Amr Alaa El Deen
 
Algorithm analysis and design
Algorithm analysis and designAlgorithm analysis and design
Algorithm analysis and designMegha V
 
Cpu performance matrix
Cpu performance matrixCpu performance matrix
Cpu performance matrixRehman baig
 
Software development best practices & coding guidelines
Software development best practices & coding guidelinesSoftware development best practices & coding guidelines
Software development best practices & coding guidelinesAnkur Goyal
 
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 structureSelf-Employed
 
Unit 1, ADA.pptx
Unit 1, ADA.pptxUnit 1, ADA.pptx
Unit 1, ADA.pptxjinkhatima
 
2. Introduction to Algorithm.pptx
2. Introduction to Algorithm.pptx2. Introduction to Algorithm.pptx
2. Introduction to Algorithm.pptxRahikAhmed1
 
Compiler optimization techniques
Compiler optimization techniquesCompiler optimization techniques
Compiler optimization techniquesHardik Devani
 

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
 
Presentation on Pandas in _ detail .pptx
Presentation on Pandas in _ detail .pptxPresentation on Pandas in _ detail .pptx
Presentation on Pandas in _ detail .pptx16115yogendraSingh
 
multipleaccesstechniques-201110153309.pptx
multipleaccesstechniques-201110153309.pptxmultipleaccesstechniques-201110153309.pptx
multipleaccesstechniques-201110153309.pptx16115yogendraSingh
 
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.pptx16115yogendraSingh
 

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

data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfJiananWang21
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
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 - VDineshKumar4165
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaOmar Fathy
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . pptDineshKumar4165
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptDineshKumar4165
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfRagavanV2
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityMorshed Ahmed Rahath
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Arindam Chakraborty, Ph.D., P.E. (CA, TX)
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueBhangaleSonal
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringmulugeta48
 
2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projectssmsksolar
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Bookingroncy bisnoi
 

Recently uploaded (20)

Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
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
 
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna Municipality
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
(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
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
(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
 
2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
 

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