SlideShare a Scribd company logo
2
Most read
4
Most read
6
Most read
Code Optimization
Presenter:
Pradip Bhattarai
Nepathya College
6th Semester
Introduction
 Optimization is a program transformation technique, which
tries to improve the code by making it consume less resources
(i.e. CPU, Memory) and deliver high speed.
 Optimizations are classified into two categories:
a) Machine independent optimizations
- improve target code without taking properties
of target machine into consideration.
b) Machine dependent optimization
Criteria for Optimization
The transformation must preserve the meaning of programs.
A transformation must, on the average, speed up programs by a
measurable amount.
The transformation must be worth the effort.
Techniques for Optimization
1) Compile time evaluation
A) Constant folding-
 As the name suggests, this technique involves folding the
constants by evaluating the expressions that involves the
operands having constant values at the compile time.
Example-
Circumference of circle = (22/7) x Diameter
 Replace (22/7) with 3.14 during compile time and save the
execution time.
1) Compile time evaluation
B) Constant Propagation-
 In this technique, if some variable has been assigned some constant
value, then it replaces that variable with its constant value in the
further program wherever it has been used during compilation,
provided that its value does not get alter in between.
Example-
pi = 3.14
radius = 10
Area of circle = pi x radius x radius
Substitutes constants value during compile time and saves the
execution time.
2) Commonsub-expressionselimination
 Eliminates the redundant expressions and avoids their
computation again and again.
Code before Optimization Code after Optimization
S1 = 4 x i
S2 = a[S1]
S3 = 4 x j
S4 = 4 x i // Redundant Expression
S5 = n
S6 = b[S4] + S5
S1 = 4 x i
S2 = a[S1]
S3 = 4 x j
S5 = n
S6 = b[S1] + S5
Example-
3) Code movement
 The code is moved out of the loop if it does not matter
whether it is present inside the loop or it is present outside
the loop.
Code before Optimization Code after Optimization
for ( int j = 0 ; j < n ; j ++)
{
x = y + z ;
a[j] = 6 x j;
}
x = y + z ;
for ( int j = 0 ; j < n ; j ++)
{
a[j] = 6 x j;
}
Example-
4) Dead code elimination
 Those code are eliminated which either never executes or are
not reachable or even if they get execute, their output is never
utilized.
Code before Optimization Code after Optimization
i = 0 ;
if (i == 1)
{
a = x + 5 ;
}
i = 0 ;
Example-
5) Strength Reduction
 As the name suggests, this technique involves reducing the strength
of the expressions by replacing the expensive and costly operators
with the simple and cheaper ones.
Example:
Here, the expression “A x 2” has been replaced with the expression
“A + A” because the cost of multiplication operator is higher than the
cost of addition operator.
Code before Optimization Code after Optimization
B = A x 2 B = A + A

More Related Content

PPTX
Principle source of optimazation
PDF
Code optimization in compiler design
PDF
Basic blocks and flow graph in Compiler Construction
PPTX
Code optimization
PPTX
Loop optimization
PPT
Type Checking(Compiler Design) #ShareThisIfYouLike
PDF
Compiler unit 2&3
PPTX
Code Optimization
Principle source of optimazation
Code optimization in compiler design
Basic blocks and flow graph in Compiler Construction
Code optimization
Loop optimization
Type Checking(Compiler Design) #ShareThisIfYouLike
Compiler unit 2&3
Code Optimization

What's hot (20)

PPTX
Peephole optimization techniques in compiler design
PPTX
Data link layer
PDF
A* Search Algorithm
PPTX
previous question solve of operating system.
PDF
Embedded C - Optimization techniques
PPTX
Dynamic storage allocation techniques in Compiler design
PPT
Code Optimization
PPTX
Compiler Design
PDF
LR(1) CLR(1) Parser with Example
PPTX
Peephole Optimization
PPTX
Class based modeling
DOCX
Flag register 8086 assignment
PDF
Syntax Directed Definition and its applications
PDF
Distributed Operating System_1
PPTX
Type checking in compiler design
PDF
I. AO* SEARCH ALGORITHM
PPT
Bus interconnection
PPTX
Parsing in Compiler Design
PDF
Instruction formats-in-8086
PPTX
Lexical analyzer generator lex
Peephole optimization techniques in compiler design
Data link layer
A* Search Algorithm
previous question solve of operating system.
Embedded C - Optimization techniques
Dynamic storage allocation techniques in Compiler design
Code Optimization
Compiler Design
LR(1) CLR(1) Parser with Example
Peephole Optimization
Class based modeling
Flag register 8086 assignment
Syntax Directed Definition and its applications
Distributed Operating System_1
Type checking in compiler design
I. AO* SEARCH ALGORITHM
Bus interconnection
Parsing in Compiler Design
Instruction formats-in-8086
Lexical analyzer generator lex
Ad

Similar to Code optimization (20)

PDF
Code Optimizatoion
PPTX
Code optimization
PPT
basics of optimizations presentation s
PPT
Code Optimization Lec#7.ppt Code Optimizer
PPTX
Code optmize.pptx which is related to coding
PPTX
complier design unit 5 for helping students
PDF
Optimization in Programming languages
PDF
PDF
PPTX
Download RarmaRadio Pro Crack Latest [2025]
PPTX
Code_Optimization_Compiler_Design software .pptx
PPTX
NCH VideoPad Pro Cracked Version Download
PPTX
Download Artweaver Plus Cracked Version Free
PPTX
PowerDirector Activated Full Tested Download
PPTX
Downlaod Wise Registry Cleaner Pro Crack
PPTX
Autodesk CFD Ultimate Crack Latest Version
PPTX
Latest TreeSize Professional 9 Crack Download
PPTX
Introduction to code optimization by dipankar
PPTX
Compiler in System Programming/Code Optimization techniques in System Program...
PDF
SPCC_Sem6_Chapter 6_Code Optimization part
Code Optimizatoion
Code optimization
basics of optimizations presentation s
Code Optimization Lec#7.ppt Code Optimizer
Code optmize.pptx which is related to coding
complier design unit 5 for helping students
Optimization in Programming languages
Download RarmaRadio Pro Crack Latest [2025]
Code_Optimization_Compiler_Design software .pptx
NCH VideoPad Pro Cracked Version Download
Download Artweaver Plus Cracked Version Free
PowerDirector Activated Full Tested Download
Downlaod Wise Registry Cleaner Pro Crack
Autodesk CFD Ultimate Crack Latest Version
Latest TreeSize Professional 9 Crack Download
Introduction to code optimization by dipankar
Compiler in System Programming/Code Optimization techniques in System Program...
SPCC_Sem6_Chapter 6_Code Optimization part
Ad

Recently uploaded (20)

PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Empathic Computing: Creating Shared Understanding
PPTX
A Presentation on Artificial Intelligence
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Encapsulation theory and applications.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Machine learning based COVID-19 study performance prediction
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
NewMind AI Weekly Chronicles - August'25 Week I
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
The AUB Centre for AI in Media Proposal.docx
Reach Out and Touch Someone: Haptics and Empathic Computing
The Rise and Fall of 3GPP – Time for a Sabbatical?
Review of recent advances in non-invasive hemoglobin estimation
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Empathic Computing: Creating Shared Understanding
A Presentation on Artificial Intelligence
Per capita expenditure prediction using model stacking based on satellite ima...
Building Integrated photovoltaic BIPV_UPV.pdf
Encapsulation theory and applications.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
20250228 LYD VKU AI Blended-Learning.pptx
Understanding_Digital_Forensics_Presentation.pptx
MYSQL Presentation for SQL database connectivity
Machine learning based COVID-19 study performance prediction
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx

Code optimization

  • 2. Introduction  Optimization is a program transformation technique, which tries to improve the code by making it consume less resources (i.e. CPU, Memory) and deliver high speed.  Optimizations are classified into two categories: a) Machine independent optimizations - improve target code without taking properties of target machine into consideration. b) Machine dependent optimization
  • 3. Criteria for Optimization The transformation must preserve the meaning of programs. A transformation must, on the average, speed up programs by a measurable amount. The transformation must be worth the effort.
  • 5. 1) Compile time evaluation A) Constant folding-  As the name suggests, this technique involves folding the constants by evaluating the expressions that involves the operands having constant values at the compile time. Example- Circumference of circle = (22/7) x Diameter  Replace (22/7) with 3.14 during compile time and save the execution time.
  • 6. 1) Compile time evaluation B) Constant Propagation-  In this technique, if some variable has been assigned some constant value, then it replaces that variable with its constant value in the further program wherever it has been used during compilation, provided that its value does not get alter in between. Example- pi = 3.14 radius = 10 Area of circle = pi x radius x radius Substitutes constants value during compile time and saves the execution time.
  • 7. 2) Commonsub-expressionselimination  Eliminates the redundant expressions and avoids their computation again and again. Code before Optimization Code after Optimization S1 = 4 x i S2 = a[S1] S3 = 4 x j S4 = 4 x i // Redundant Expression S5 = n S6 = b[S4] + S5 S1 = 4 x i S2 = a[S1] S3 = 4 x j S5 = n S6 = b[S1] + S5 Example-
  • 8. 3) Code movement  The code is moved out of the loop if it does not matter whether it is present inside the loop or it is present outside the loop. Code before Optimization Code after Optimization for ( int j = 0 ; j < n ; j ++) { x = y + z ; a[j] = 6 x j; } x = y + z ; for ( int j = 0 ; j < n ; j ++) { a[j] = 6 x j; } Example-
  • 9. 4) Dead code elimination  Those code are eliminated which either never executes or are not reachable or even if they get execute, their output is never utilized. Code before Optimization Code after Optimization i = 0 ; if (i == 1) { a = x + 5 ; } i = 0 ; Example-
  • 10. 5) Strength Reduction  As the name suggests, this technique involves reducing the strength of the expressions by replacing the expensive and costly operators with the simple and cheaper ones. Example: Here, the expression “A x 2” has been replaced with the expression “A + A” because the cost of multiplication operator is higher than the cost of addition operator. Code before Optimization Code after Optimization B = A x 2 B = A + A