SlideShare a Scribd company logo
4
Most read
6
Most read
10
Most read
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
Loop optimization

More Related Content

What's hot (20)

PPTX
sum of subset problem using Backtracking
Abhishek Singh
 
PPTX
Recognition-of-tokens
Dattatray Gandhmal
 
PPTX
Code Optimization
Akhil Kaushik
 
PPTX
Breadth First Search & Depth First Search
Kevin Jadiya
 
PPT
Symbol table management and error handling in compiler design
Swati Chauhan
 
PPTX
Introduction to Dynamic Programming, Principle of Optimality
Bhavin Darji
 
PPTX
The n Queen Problem
Sukrit Gupta
 
PDF
I.BEST FIRST SEARCH IN AI
vikas dhakane
 
PPTX
Sum of subset problem.pptx
V.V.Vanniaperumal College for Women
 
PPTX
Error Detection & Recovery
Akhil Kaushik
 
PPTX
Control Strategies in AI
Bharat Bhushan
 
PPTX
Computer system architecture
venkateswarlu G
 
PPTX
Cpu scheduling in operating System.
Ravi Kumar Patel
 
PDF
Intermediate code generation in Compiler Design
Kuppusamy P
 
PPTX
Scheduling Definition, objectives and types
Maitree Patel
 
PPTX
Dfs presentation
Alizay Khan
 
PPT
Intermediate code generation (Compiler Design)
Tasif Tanzim
 
PPTX
Round robin scheduling
Raghav S
 
PPT
Heuristc Search Techniques
Jismy .K.Jose
 
PPT
Randomized algorithms ver 1.0
Dr. C.V. Suresh Babu
 
sum of subset problem using Backtracking
Abhishek Singh
 
Recognition-of-tokens
Dattatray Gandhmal
 
Code Optimization
Akhil Kaushik
 
Breadth First Search & Depth First Search
Kevin Jadiya
 
Symbol table management and error handling in compiler design
Swati Chauhan
 
Introduction to Dynamic Programming, Principle of Optimality
Bhavin Darji
 
The n Queen Problem
Sukrit Gupta
 
I.BEST FIRST SEARCH IN AI
vikas dhakane
 
Sum of subset problem.pptx
V.V.Vanniaperumal College for Women
 
Error Detection & Recovery
Akhil Kaushik
 
Control Strategies in AI
Bharat Bhushan
 
Computer system architecture
venkateswarlu G
 
Cpu scheduling in operating System.
Ravi Kumar Patel
 
Intermediate code generation in Compiler Design
Kuppusamy P
 
Scheduling Definition, objectives and types
Maitree Patel
 
Dfs presentation
Alizay Khan
 
Intermediate code generation (Compiler Design)
Tasif Tanzim
 
Round robin scheduling
Raghav S
 
Heuristc Search Techniques
Jismy .K.Jose
 
Randomized algorithms ver 1.0
Dr. C.V. Suresh Babu
 

Similar to Loop optimization (20)

PPTX
Code optmize.pptx which is related to coding
vamami6395
 
PPTX
complier design unit 5 for helping students
aniketsugandhi1
 
PDF
Optimization in Programming languages
Ankit Pandey
 
PPTX
Code optimization
Pradip Bhattarai
 
PPT
Code Optimization Lec#7.ppt Code Optimizer
zeeshanmubeen1
 
PPTX
Compiler presention
Faria Priya
 
PPTX
Compiler optimization techniques
Hardik Devani
 
PDF
Code tuning techniques
Asha Sari
 
PPT
lect23_optimization.ppt
ssuser0be977
 
PDF
Optimization
Royalzig Luxury Furniture
 
PDF
Optimization
Royalzig Luxury Furniture
 
PPT
basics of optimizations presentation s
AnkitKumarSharma26
 
PPT
457418.-Compiler-Design-Code-optimization.ppt
Incredible20
 
PPTX
Compiler Design theory and various phases of compiler.pptx
aabbpy249
 
PPTX
Preliminary transformations
🐸 Ashish Taldeokar
 
PPTX
Compiler Optimization Presentation
19magnet
 
PPTX
Code optimization
Sameenafathima4
 
PPT
code optimization
Sanjeev Raaz
 
PPT
code optimization 1...code optimization-1221849738688969-9
Sanjeev Raaz
 
PPT
Code Optimization
guest9f8315
 
Code optmize.pptx which is related to coding
vamami6395
 
complier design unit 5 for helping students
aniketsugandhi1
 
Optimization in Programming languages
Ankit Pandey
 
Code optimization
Pradip Bhattarai
 
Code Optimization Lec#7.ppt Code Optimizer
zeeshanmubeen1
 
Compiler presention
Faria Priya
 
Compiler optimization techniques
Hardik Devani
 
Code tuning techniques
Asha Sari
 
lect23_optimization.ppt
ssuser0be977
 
basics of optimizations presentation s
AnkitKumarSharma26
 
457418.-Compiler-Design-Code-optimization.ppt
Incredible20
 
Compiler Design theory and various phases of compiler.pptx
aabbpy249
 
Preliminary transformations
🐸 Ashish Taldeokar
 
Compiler Optimization Presentation
19magnet
 
Code optimization
Sameenafathima4
 
code optimization
Sanjeev Raaz
 
code optimization 1...code optimization-1221849738688969-9
Sanjeev Raaz
 
Code Optimization
guest9f8315
 
Ad

Recently uploaded (20)

PDF
Zilliz Cloud Demo for performance and scale
Zilliz
 
PPTX
Element 7. CHEMICAL AND BIOLOGICAL AGENT.pptx
merrandomohandas
 
PPTX
Depth First Search Algorithm in 🧠 DFS in Artificial Intelligence (AI)
rafeeqshaik212002
 
PDF
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
PPTX
Shinkawa Proposal to meet Vibration API670.pptx
AchmadBashori2
 
PDF
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
PPTX
Hashing Introduction , hash functions and techniques
sailajam21
 
PPTX
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
PPTX
MobileComputingMANET2023 MobileComputingMANET2023.pptx
masterfake98765
 
PDF
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
PPTX
artificial intelligence applications in Geomatics
NawrasShatnawi1
 
PPTX
VITEEE 2026 Exam Details , Important Dates
SonaliSingh127098
 
PDF
Electrical Engineer operation Supervisor
ssaruntatapower143
 
DOC
MRRS Strength and Durability of Concrete
CivilMythili
 
PPTX
Server Side Web Development Unit 1 of Nodejs.pptx
sneha852132
 
PPTX
Introduction to Design of Machine Elements
PradeepKumarS27
 
PPTX
Damage of stability of a ship and how its change .pptx
ehamadulhaque
 
PDF
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
DOCX
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
PDF
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
Zilliz Cloud Demo for performance and scale
Zilliz
 
Element 7. CHEMICAL AND BIOLOGICAL AGENT.pptx
merrandomohandas
 
Depth First Search Algorithm in 🧠 DFS in Artificial Intelligence (AI)
rafeeqshaik212002
 
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
Shinkawa Proposal to meet Vibration API670.pptx
AchmadBashori2
 
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
Hashing Introduction , hash functions and techniques
sailajam21
 
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
MobileComputingMANET2023 MobileComputingMANET2023.pptx
masterfake98765
 
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
artificial intelligence applications in Geomatics
NawrasShatnawi1
 
VITEEE 2026 Exam Details , Important Dates
SonaliSingh127098
 
Electrical Engineer operation Supervisor
ssaruntatapower143
 
MRRS Strength and Durability of Concrete
CivilMythili
 
Server Side Web Development Unit 1 of Nodejs.pptx
sneha852132
 
Introduction to Design of Machine Elements
PradeepKumarS27
 
Damage of stability of a ship and how its change .pptx
ehamadulhaque
 
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
Ad

Loop optimization

  • 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