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

What's hot (20)

PPTX
Syntax Analysis in Compiler Design
MAHASREEM
 
PPTX
Three Address code
Pooja Dixit
 
PPTX
Intermediate code- generation
rawan_z
 
PDF
Embedded C - Optimization techniques
Emertxe Information Technologies Pvt Ltd
 
PPTX
Distributed Shared Memory
Prakhar Rastogi
 
PPS
Virtual memory
Anuj Modi
 
PPTX
Pipelining And Vector Processing
TheInnocentTuber
 
PDF
Note 6
MejbahUddinRafi
 
PDF
Intermediate code generation in Compiler Design
Kuppusamy P
 
PPTX
Loops in flow
indhu mathi
 
PPT
Compiler Design Unit 5
Jena Catherine Bel D
 
PPTX
Peephole optimization techniques in compiler design
Anul Chaudhary
 
PPTX
Optimization of basic blocks
ishwarya516
 
PPTX
MODES OF TRANSFER.pptx
22X047SHRISANJAYM
 
PPT
Instruction format
Sanjeev Patel
 
PPT
Parallel processing
Syed Zaid Irshad
 
PPTX
Peephole Optimization
United International University
 
PPTX
Code generation
Aparna Nayak
 
PPTX
Code optimization
veena venugopal
 
PDF
Chomsky hierarchy
SANUC2
 
Syntax Analysis in Compiler Design
MAHASREEM
 
Three Address code
Pooja Dixit
 
Intermediate code- generation
rawan_z
 
Embedded C - Optimization techniques
Emertxe Information Technologies Pvt Ltd
 
Distributed Shared Memory
Prakhar Rastogi
 
Virtual memory
Anuj Modi
 
Pipelining And Vector Processing
TheInnocentTuber
 
Intermediate code generation in Compiler Design
Kuppusamy P
 
Loops in flow
indhu mathi
 
Compiler Design Unit 5
Jena Catherine Bel D
 
Peephole optimization techniques in compiler design
Anul Chaudhary
 
Optimization of basic blocks
ishwarya516
 
MODES OF TRANSFER.pptx
22X047SHRISANJAYM
 
Instruction format
Sanjeev Patel
 
Parallel processing
Syed Zaid Irshad
 
Peephole Optimization
United International University
 
Code generation
Aparna Nayak
 
Code optimization
veena venugopal
 
Chomsky hierarchy
SANUC2
 

Similar to Code optimization (20)

PDF
Code Optimizatoion
rajuvermadsvv
 
PPTX
Code optimization
Sameenafathima4
 
PPTX
complier design unit 5 for helping students
aniketsugandhi1
 
PDF
Optimization in Programming languages
Ankit Pandey
 
PDF
SPCC_Sem6_Chapter 6_Code Optimization part
NiramayKolalle
 
PPTX
Code optmize.pptx which is related to coding
vamami6395
 
PDF
Compiler Design- Machine Independent Optimizations
Jyothishmathi Institute of Technology and Science Karimnagar
 
PPT
UNIT-2-PPTS-DAA.ppt
GovindUpadhyay25
 
PDF
TSR CLASS CD-UNIT 5.pdf sddfsfdsfqweqdew
vtu21524
 
PPT
basics of optimizations presentation s
AnkitKumarSharma26
 
PPTX
CODE TUNINGtertertertrtryryryryrtytrytrtry
kapib57390
 
PDF
Optimization
Royalzig Luxury Furniture
 
PDF
Optimization
Royalzig Luxury Furniture
 
PPTX
Peephole optimization techniques
garishma bhatia
 
PPTX
2-Algorithms and Complexity analysis.pptx
231b209
 
PPT
Code Optimization Lec#7.ppt Code Optimizer
zeeshanmubeen1
 
PPTX
compiler design-Intermediate code generation.pptx
murudkarp11
 
PPTX
Introduction to code optimization by dipankar
Dipankar Nalui
 
PDF
UNIT-1-PdjfjfjfjfjfjfjfjfjfjfjPTS-DAA.pdf
NagendraK18
 
PDF
UNIT-1-PPTS-DAA_cofjfjvjcjcncnfncmpressed.pdf
NagendraK18
 
Code Optimizatoion
rajuvermadsvv
 
Code optimization
Sameenafathima4
 
complier design unit 5 for helping students
aniketsugandhi1
 
Optimization in Programming languages
Ankit Pandey
 
SPCC_Sem6_Chapter 6_Code Optimization part
NiramayKolalle
 
Code optmize.pptx which is related to coding
vamami6395
 
Compiler Design- Machine Independent Optimizations
Jyothishmathi Institute of Technology and Science Karimnagar
 
UNIT-2-PPTS-DAA.ppt
GovindUpadhyay25
 
TSR CLASS CD-UNIT 5.pdf sddfsfdsfqweqdew
vtu21524
 
basics of optimizations presentation s
AnkitKumarSharma26
 
CODE TUNINGtertertertrtryryryryrtytrytrtry
kapib57390
 
Peephole optimization techniques
garishma bhatia
 
2-Algorithms and Complexity analysis.pptx
231b209
 
Code Optimization Lec#7.ppt Code Optimizer
zeeshanmubeen1
 
compiler design-Intermediate code generation.pptx
murudkarp11
 
Introduction to code optimization by dipankar
Dipankar Nalui
 
UNIT-1-PdjfjfjfjfjfjfjfjfjfjfjPTS-DAA.pdf
NagendraK18
 
UNIT-1-PPTS-DAA_cofjfjvjcjcncnfncmpressed.pdf
NagendraK18
 
Ad

Recently uploaded (20)

PDF
Learn Computer Forensics, Second Edition
AnuraShantha7
 
PDF
SFWelly Summer 25 Release Highlights July 2025
Anna Loughnan Colquhoun
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PDF
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
PDF
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PDF
Blockchain Transactions Explained For Everyone
CIFDAQ
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PPTX
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
Learn Computer Forensics, Second Edition
AnuraShantha7
 
SFWelly Summer 25 Release Highlights July 2025
Anna Loughnan Colquhoun
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
Blockchain Transactions Explained For Everyone
CIFDAQ
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
Ad

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