SlideShare a Scribd company logo
1 of 11
NADAR SARASWATHI COLLEGE
OF ARTS AND SCIENCE
DEPARTMENT OF COMPUTER SCIENCE
COMPILER DESIGN
CODE OPTIMIZATION
BY
S.SAMEENA FATHIMA
I-MSC(CS)
WHAT IS CODE OPTIMIZATION?
Contd...
Code optimization is a program modification strategy
that endeavours to enhance the intermediate code, so a
program utilises the least potential memory, minimises
its CPU time and offers high speed.
The process of code optimization involves-
• Eliminating the unwanted code lines
• Rearranging the statements of the code
Advantages:
• The optimized code has the
following advantages-
• Optimized code has faster
execution speed.
• Optimized code utilizes the
memory efficiently.
• Optimized code gives better
performance.
Code Optimization Techniques-
1. Compile Time Evaluation
2. Common sub-expression
elimination
3. Dead Code Elimination
4. Code Movement
5. Strength Reduction
1. Compile Time Evaluation-
Two techniques that falls under compile time evaluation are-
A) Constant Folding-
• In this technique,
• As the name suggests, it involves folding the constants.
• The expressions that contain the operands having constant
values at compile time are evaluated.
• Those expressions are then replaced with their respective
results.
Example-
Circumference of Circle = (22/7) x Diameter
Here,
This technique evaluates the expression 22/7 at compile time.
The expression is then replaced with its result 3.14.
This saves the time at run time.
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 during
compilation.
• The condition is that the value of variable must not get alter in between.
Example-
• pi = 3.14
• radius = 10
• Area of circle = pi x radius x radius
• Here,
• This technique substitutes the value of variables ‘pi’ and ‘radius’ at compile
time.
• It then evaluates the expression 3.14 x 10 x 10.
• The expression is then replaced with its result 314.
• This saves the time at run time.
The expression that has been already computed before and appears
again in the code for computation
is called as Common Sub-Expression.
2. Common Sub-Expression Elimination-
In this technique,
•As the name suggests, it involves eliminating the common sub expressions.
•The redundant expressions are eliminated to avoid their re-computation.
•The already computed result is used in the further program when required.
3. Code Movement-
• In this technique,
• As the name suggests, it involves movement of the code.
• The code present inside the loop is moved out if it does not matter whether it is
present inside or outside.
• Such a code unnecessarily gets execute again and again with each iteration of the
loop.
• This leads to the wastage of time at run time.
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;
}
4. Dead Code Elimination-
• In this technique,
• As the name suggests, it involves eliminating the dead code.
• The statements of the code which either never executes or are unreachable
or their output is never used are eliminated.
Code Before Optimization Code After Optimization
i = 0 ;
if (i == 1)
{
a = x + 5 ;
}
i = 0 ;
5. Strength Reduction-
• In this technique,
• As the name suggests, it involves reducing the strength of expressions.
• This technique replaces the expensive and costly operators with the
simple and cheaper ones.
Code Before Optimization Code After Optimization
B = A x 2 B = A + A
Here,
• The expression “A x 2” is replaced with the expression “A + A”.
• This is because the cost of multiplication operator is higher than that
of addition operator.
THANK YOU !

More Related Content

Similar to Code optimization

Unit 1, ADA.pptx
Unit 1, ADA.pptxUnit 1, ADA.pptx
Unit 1, ADA.pptxjinkhatima
 
VCE Unit 01 (1).pptx
VCE Unit 01 (1).pptxVCE Unit 01 (1).pptx
VCE Unit 01 (1).pptxskilljiolms
 
02 functions, variables, basic input and output of c++
02   functions, variables, basic input and output of c++02   functions, variables, basic input and output of c++
02 functions, variables, basic input and output of c++Manzoor ALam
 
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
 
Pragmatic Optimization in Modern Programming - Ordering Optimization Approaches
Pragmatic Optimization in Modern Programming - Ordering Optimization ApproachesPragmatic Optimization in Modern Programming - Ordering Optimization Approaches
Pragmatic Optimization in Modern Programming - Ordering Optimization ApproachesMarina Kolpakova
 
Introduction to Algorithms Complexity Analysis
Introduction to Algorithms Complexity Analysis Introduction to Algorithms Complexity Analysis
Introduction to Algorithms Complexity Analysis Dr. Pankaj Agarwal
 
Compiler presention
Compiler presentionCompiler presention
Compiler presentionFaria Priya
 
Analysis and Design of Algorithms
Analysis and Design of AlgorithmsAnalysis and Design of Algorithms
Analysis and Design of AlgorithmsBulbul Agrawal
 
Building source code level profiler for C++.pdf
Building source code level profiler for C++.pdfBuilding source code level profiler for C++.pdf
Building source code level profiler for C++.pdfssuser28de9e
 
Design & Analysis of Algorithm course .pptx
Design & Analysis of Algorithm course .pptxDesign & Analysis of Algorithm course .pptx
Design & Analysis of Algorithm course .pptxJeevaMCSEKIOT
 
Inline function
Inline functionInline function
Inline functionTech_MX
 

Similar to Code optimization (20)

Introduction to C ++.pptx
Introduction to C ++.pptxIntroduction to C ++.pptx
Introduction to C ++.pptx
 
UNIT-2-PPTS-DAA.ppt
UNIT-2-PPTS-DAA.pptUNIT-2-PPTS-DAA.ppt
UNIT-2-PPTS-DAA.ppt
 
Unit 1, ADA.pptx
Unit 1, ADA.pptxUnit 1, ADA.pptx
Unit 1, ADA.pptx
 
VCE Unit 01 (1).pptx
VCE Unit 01 (1).pptxVCE Unit 01 (1).pptx
VCE Unit 01 (1).pptx
 
02 functions, variables, basic input and output of c++
02   functions, variables, basic input and output of c++02   functions, variables, basic input and output of c++
02 functions, variables, basic input and output of c++
 
sCode optimization
sCode optimizationsCode optimization
sCode optimization
 
Basic Elements of C++
Basic Elements of C++Basic Elements of C++
Basic Elements of C++
 
Code Tuning
Code TuningCode Tuning
Code Tuning
 
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
 
Pragmatic Optimization in Modern Programming - Ordering Optimization Approaches
Pragmatic Optimization in Modern Programming - Ordering Optimization ApproachesPragmatic Optimization in Modern Programming - Ordering Optimization Approaches
Pragmatic Optimization in Modern Programming - Ordering Optimization Approaches
 
Introduction to Algorithms Complexity Analysis
Introduction to Algorithms Complexity Analysis Introduction to Algorithms Complexity Analysis
Introduction to Algorithms Complexity Analysis
 
85ec7 session2 c++
85ec7 session2 c++85ec7 session2 c++
85ec7 session2 c++
 
Compiler presention
Compiler presentionCompiler presention
Compiler presention
 
Compiler Design- Machine Independent Optimizations
Compiler Design- Machine Independent OptimizationsCompiler Design- Machine Independent Optimizations
Compiler Design- Machine Independent Optimizations
 
Optimization
OptimizationOptimization
Optimization
 
Optimization
OptimizationOptimization
Optimization
 
Analysis and Design of Algorithms
Analysis and Design of AlgorithmsAnalysis and Design of Algorithms
Analysis and Design of Algorithms
 
Building source code level profiler for C++.pdf
Building source code level profiler for C++.pdfBuilding source code level profiler for C++.pdf
Building source code level profiler for C++.pdf
 
Design & Analysis of Algorithm course .pptx
Design & Analysis of Algorithm course .pptxDesign & Analysis of Algorithm course .pptx
Design & Analysis of Algorithm course .pptx
 
Inline function
Inline functionInline function
Inline function
 

More from Sameenafathima4 (15)

ai.pptx
ai.pptxai.pptx
ai.pptx
 
dip.pptx
dip.pptxdip.pptx
dip.pptx
 
iot.pptx
iot.pptxiot.pptx
iot.pptx
 
women empowerment.pptx
women empowerment.pptxwomen empowerment.pptx
women empowerment.pptx
 
CLOUD COMPUTING.pptx
CLOUD COMPUTING.pptxCLOUD COMPUTING.pptx
CLOUD COMPUTING.pptx
 
crypto.pptx
crypto.pptxcrypto.pptx
crypto.pptx
 
crypto.pptx
crypto.pptxcrypto.pptx
crypto.pptx
 
nosql.pptx
nosql.pptxnosql.pptx
nosql.pptx
 
Features of Android OS.pptx
Features of Android OS.pptxFeatures of Android OS.pptx
Features of Android OS.pptx
 
python p.pptx
python p.pptxpython p.pptx
python p.pptx
 
Creating windows
Creating windowsCreating windows
Creating windows
 
Swing components
Swing components Swing components
Swing components
 
dsa.pptx
dsa.pptxdsa.pptx
dsa.pptx
 
anatomy of a jsp page & jsp syntax.pptx
anatomy of a jsp page & jsp syntax.pptxanatomy of a jsp page & jsp syntax.pptx
anatomy of a jsp page & jsp syntax.pptx
 
JAVA SERVER PAGE
JAVA SERVER PAGEJAVA SERVER PAGE
JAVA SERVER PAGE
 

Recently uploaded

Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Principled Technologies
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 

Recently uploaded (20)

Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 

Code optimization

  • 1. NADAR SARASWATHI COLLEGE OF ARTS AND SCIENCE DEPARTMENT OF COMPUTER SCIENCE COMPILER DESIGN CODE OPTIMIZATION BY S.SAMEENA FATHIMA I-MSC(CS)
  • 2. WHAT IS CODE OPTIMIZATION? Contd... Code optimization is a program modification strategy that endeavours to enhance the intermediate code, so a program utilises the least potential memory, minimises its CPU time and offers high speed. The process of code optimization involves- • Eliminating the unwanted code lines • Rearranging the statements of the code
  • 3. Advantages: • The optimized code has the following advantages- • Optimized code has faster execution speed. • Optimized code utilizes the memory efficiently. • Optimized code gives better performance.
  • 4. Code Optimization Techniques- 1. Compile Time Evaluation 2. Common sub-expression elimination 3. Dead Code Elimination 4. Code Movement 5. Strength Reduction
  • 5. 1. Compile Time Evaluation- Two techniques that falls under compile time evaluation are- A) Constant Folding- • In this technique, • As the name suggests, it involves folding the constants. • The expressions that contain the operands having constant values at compile time are evaluated. • Those expressions are then replaced with their respective results. Example- Circumference of Circle = (22/7) x Diameter Here, This technique evaluates the expression 22/7 at compile time. The expression is then replaced with its result 3.14. This saves the time at run time.
  • 6. 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 during compilation. • The condition is that the value of variable must not get alter in between. Example- • pi = 3.14 • radius = 10 • Area of circle = pi x radius x radius • Here, • This technique substitutes the value of variables ‘pi’ and ‘radius’ at compile time. • It then evaluates the expression 3.14 x 10 x 10. • The expression is then replaced with its result 314. • This saves the time at run time.
  • 7. The expression that has been already computed before and appears again in the code for computation is called as Common Sub-Expression. 2. Common Sub-Expression Elimination- In this technique, •As the name suggests, it involves eliminating the common sub expressions. •The redundant expressions are eliminated to avoid their re-computation. •The already computed result is used in the further program when required.
  • 8. 3. Code Movement- • In this technique, • As the name suggests, it involves movement of the code. • The code present inside the loop is moved out if it does not matter whether it is present inside or outside. • Such a code unnecessarily gets execute again and again with each iteration of the loop. • This leads to the wastage of time at run time. 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; }
  • 9. 4. Dead Code Elimination- • In this technique, • As the name suggests, it involves eliminating the dead code. • The statements of the code which either never executes or are unreachable or their output is never used are eliminated. Code Before Optimization Code After Optimization i = 0 ; if (i == 1) { a = x + 5 ; } i = 0 ;
  • 10. 5. Strength Reduction- • In this technique, • As the name suggests, it involves reducing the strength of expressions. • This technique replaces the expensive and costly operators with the simple and cheaper ones. Code Before Optimization Code After Optimization B = A x 2 B = A + A Here, • The expression “A x 2” is replaced with the expression “A + A”. • This is because the cost of multiplication operator is higher than that of addition operator.