SlideShare a Scribd company logo
1 of 24
Akhil Kaushik
Asstt. Prof., CE Deptt.,
TIT Bhiwani
Code Optimization
Optimization
• In computing, optimization is the process of modifying a
system to make some aspect of it work more efficiently or
use fewer resources.
• Ex: a computer program may be optimized so that it
executes more rapidly or is capable of operating with less
memory storage or other resources, or draw less power.
• The system may be a single computer program, a
collection of computers or even an entire network such as
the internet.
Code Optimization
• Optimization is a program transformation technique,
which tries to improve the code that consume less
resources (i.e. CPU, Memory) and deliver high speed.
• In optimization, high-level general programming
constructs are replaced by very efficient low-level
programming codes.
• Usually done at the end of the development stage since
it reduces readability & adds code that is used to
improve the performance.
Code Optimization
Optimized code’s features:-
• Executes faster
• Code size get reduced
• Efficient memory usage
• Yielding better performance
• Reduces the time and space complexity
Code Optimization
Optimizations are classified into two categories:
• Machine independent optimizations - improve target
code without taking properties of target machine into
consideration.
• Machine dependent optimization - improve target
code by checking properties of target machine.
Code Optimization
Criteria for Optimization:-
• An optimization must preserve the meaning of a
program:
-Cannot change the output produced for any input.
-Can not introduce an error.
• Optimization should, on average, speed up programs.
• Optimization should itself be fast and should not delay
the overall compiling process.
Code Optimization
Optimization can occur at several levels:
1.Design level:- At the highest level, the design may be
optimized to make best use of the available resources.
– The implementation of this design will benefit from the use of
suitable efficient algorithms and the implementation of these
algorithms will benefit from writing good quality code.
2.Compile level:- Use of an optimizing compiler tends to
ensure that the executable program is optimized at least as
much as the compiler can predict
Code Optimization
Improvements can be made at various phases:-
• Source Code: Algorithm's transformation can produce
spectacular improvements
• Intermediate Code: Compiler can improve loops,
procedure calls and address calculations.
-Typically only optimizing compilers include this phase
• Target Code: Compilers can use registers efficiently
Code Optimization
• Main techniques of Optimization:-
Code Optimization
There are primarily 3 types of optimizations:-
(1) Local optimization - Apply to a basic block in isolation
(2) Global optimization - Apply across basic blocks
(3) Loop optimization – Apply on loops
Another significant optimization can be:-
(4) peep-hole optimization - Apply across boundaries
Code Optimization
• Optimization performed within a basic block.
• This is simplest form of optimizations
• No need to analyze the whole procedure body.
• Just analyze the basic blocks of the procedure.
• The local optimization techniques include:
– Constant Folding
– Constant Propagation
– Algebraic Simplification
– Operator Strength Reduction
– Dead Code Elimination
Local Code Optimization
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.
• Ex: Circumference of circle = (22/7) x Diameter
• Replace (22/7) with 3.14 during compile time and save
the execution time.
Local Code Optimization
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.
• Ex: pi = 3.14, radius = 10
• Area of circle = pi x radius x radius
• Substitutes constants value during compile time and
saves the execution time.
Local Code Optimization
C) Algebraic Simplification-
• Use algebraic properties to simplify expressions
• Some expressions can be simplified by replacing them
with an equivalent expression that is more efficient.
• Ex:
Code Optimization
D) 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
Code Optimization
E) 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 Optimization
F) Common sub-expressions elimination:
• Eliminates the redundant expressions and avoids their
computation again and again.
Global Code Optimization
• Optimization across basic blocks within a procedure /function
• Could be restricted to a smaller scope, Example: a loop
• Data-flow analysis is done to perform optimization across basic
blocks
• Each basic block is a node in the flow graph of the program.
• These optimizations can be extended to an entire control - flow
graph
• Most of compiler implement global optimizations with well
founded theory and practical gains
Peephole Optimization
• Optimization technique that operates on the one or
few instructions at a time.
• Performs machine dependent improvements
• Peeps into a single or sequence of two to three
instructions (peephole) and replaces it by most
efficient alternative (shorter or faster) instructions.
• Peephole is a small moving window on the target
systems
Peephole Optimization
Characteristics of peep-hole optimizations:-
• Redundant-instruction (loads and stores)elimination
• Flow-of-control optimizations - - Elimination of
multiple jumps (i.e. goto statements)
• Elimination of unreachable code
• Algebraic simplifications
• Reducing operator strength
• Use of machine idioms
Loop Optimization
• Loop optimization plays an important role in
improving the performance of the source code by
reducing overheads associated with executing
loops.
• Loop Optimization can be done by removing:
–Loop invariant
–Induction variables
Loop Optimization
• Removal of Loop Invariant data:-
Loop Optimization
• Induction variables:-
Akhil Kaushik
akhilkaushik05@gmail.com
9416910303
CONTACT ME AT:
Akhil Kaushik
akhilkaushik05@gmail.com
9416910303
THANK YOU !!!

More Related Content

What's hot

Principle source of optimazation
Principle source of optimazationPrinciple source of optimazation
Principle source of optimazationSiva Sathya
 
Symbol table in compiler Design
Symbol table in compiler DesignSymbol table in compiler Design
Symbol table in compiler DesignKuppusamy P
 
Three Address code
Three Address code Three Address code
Three Address code Pooja Dixit
 
Principal source of optimization in compiler design
Principal source of optimization in compiler designPrincipal source of optimization in compiler design
Principal source of optimization in compiler designRajkumar R
 
Data Encryption Standard (DES)
Data Encryption Standard (DES)Data Encryption Standard (DES)
Data Encryption Standard (DES)Haris Ahmed
 
I. AO* SEARCH ALGORITHM
I. AO* SEARCH ALGORITHMI. AO* SEARCH ALGORITHM
I. AO* SEARCH ALGORITHMvikas dhakane
 
Code optimization in compiler design
Code optimization in compiler designCode optimization in compiler design
Code optimization in compiler designKuppusamy P
 
Code generator
Code generatorCode generator
Code generatorTech_MX
 
Single pass assembler
Single pass assemblerSingle pass assembler
Single pass assemblerBansari Shah
 
Token, Pattern and Lexeme
Token, Pattern and LexemeToken, Pattern and Lexeme
Token, Pattern and LexemeA. S. M. Shafi
 
Intermediate code generation in Compiler Design
Intermediate code generation in Compiler DesignIntermediate code generation in Compiler Design
Intermediate code generation in Compiler DesignKuppusamy P
 
Finite automata-for-lexical-analysis
Finite automata-for-lexical-analysisFinite automata-for-lexical-analysis
Finite automata-for-lexical-analysisDattatray Gandhmal
 
Register allocation and assignment
Register allocation and assignmentRegister allocation and assignment
Register allocation and assignmentKarthi Keyan
 

What's hot (20)

Principle source of optimazation
Principle source of optimazationPrinciple source of optimazation
Principle source of optimazation
 
Symbol table in compiler Design
Symbol table in compiler DesignSymbol table in compiler Design
Symbol table in compiler Design
 
Three Address code
Three Address code Three Address code
Three Address code
 
Principal source of optimization in compiler design
Principal source of optimization in compiler designPrincipal source of optimization in compiler design
Principal source of optimization in compiler design
 
Data Encryption Standard (DES)
Data Encryption Standard (DES)Data Encryption Standard (DES)
Data Encryption Standard (DES)
 
Introduction to Compiler design
Introduction to Compiler design Introduction to Compiler design
Introduction to Compiler design
 
I. AO* SEARCH ALGORITHM
I. AO* SEARCH ALGORITHMI. AO* SEARCH ALGORITHM
I. AO* SEARCH ALGORITHM
 
Loop optimization
Loop optimizationLoop optimization
Loop optimization
 
Code optimization in compiler design
Code optimization in compiler designCode optimization in compiler design
Code optimization in compiler design
 
Compiler Chapter 1
Compiler Chapter 1Compiler Chapter 1
Compiler Chapter 1
 
Input-Buffering
Input-BufferingInput-Buffering
Input-Buffering
 
Unit 4 sp macro
Unit 4 sp macroUnit 4 sp macro
Unit 4 sp macro
 
Code generator
Code generatorCode generator
Code generator
 
COMPILER DESIGN Run-Time Environments
COMPILER DESIGN Run-Time EnvironmentsCOMPILER DESIGN Run-Time Environments
COMPILER DESIGN Run-Time Environments
 
Type Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLikeType Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLike
 
Single pass assembler
Single pass assemblerSingle pass assembler
Single pass assembler
 
Token, Pattern and Lexeme
Token, Pattern and LexemeToken, Pattern and Lexeme
Token, Pattern and Lexeme
 
Intermediate code generation in Compiler Design
Intermediate code generation in Compiler DesignIntermediate code generation in Compiler Design
Intermediate code generation in Compiler Design
 
Finite automata-for-lexical-analysis
Finite automata-for-lexical-analysisFinite automata-for-lexical-analysis
Finite automata-for-lexical-analysis
 
Register allocation and assignment
Register allocation and assignmentRegister allocation and assignment
Register allocation and assignment
 

Similar to Code Optimization

Peephole optimization techniques
Peephole optimization techniquesPeephole optimization techniques
Peephole optimization techniquesgarishma bhatia
 
Unit 1 Computer organization and Instructions
Unit 1 Computer organization and InstructionsUnit 1 Computer organization and Instructions
Unit 1 Computer organization and InstructionsBalaji Vignesh
 
Cse viii-advanced-computer-architectures-06cs81-solution
Cse viii-advanced-computer-architectures-06cs81-solutionCse viii-advanced-computer-architectures-06cs81-solution
Cse viii-advanced-computer-architectures-06cs81-solutionShobha Kumar
 
Parallel Computing - Lec 6
Parallel Computing - Lec 6Parallel Computing - Lec 6
Parallel Computing - Lec 6Shah Zaib
 
L-2 (Computer Performance).ppt
L-2 (Computer Performance).pptL-2 (Computer Performance).ppt
L-2 (Computer Performance).pptImranKhan997082
 
Basics of micro controllers for biginners
Basics of  micro controllers for biginnersBasics of  micro controllers for biginners
Basics of micro controllers for biginnersGerwin Makanyanga
 
Project Life Cycle and Effort Estimation
Project Life Cycle andEffort EstimationProject Life Cycle andEffort Estimation
Project Life Cycle and Effort Estimationssuserb7c8b8
 
Algorithmic problem sloving
Algorithmic problem slovingAlgorithmic problem sloving
Algorithmic problem slovingMani Kandan
 
Practical C++ Generative Programming
Practical C++ Generative ProgrammingPractical C++ Generative Programming
Practical C++ Generative ProgrammingSchalk Cronjé
 
ScilabTEC 2015 - Noesis Solutions
ScilabTEC 2015 - Noesis SolutionsScilabTEC 2015 - Noesis Solutions
ScilabTEC 2015 - Noesis SolutionsScilab
 
Introduction to embedded computing and arm processors
Introduction to embedded computing and arm processorsIntroduction to embedded computing and arm processors
Introduction to embedded computing and arm processorsSiva Kumar
 
COCOMO 1 Model ppt AR-1.pdf
COCOMO 1 Model  ppt AR-1.pdfCOCOMO 1 Model  ppt AR-1.pdf
COCOMO 1 Model ppt AR-1.pdf23017156038
 
pipeline in computer architecture design
pipeline in computer architecture  designpipeline in computer architecture  design
pipeline in computer architecture designssuser87fa0c1
 

Similar to Code Optimization (20)

Peephole optimization techniques
Peephole optimization techniquesPeephole optimization techniques
Peephole optimization techniques
 
Unit 1 Computer organization and Instructions
Unit 1 Computer organization and InstructionsUnit 1 Computer organization and Instructions
Unit 1 Computer organization and Instructions
 
Code optimization
Code optimizationCode optimization
Code optimization
 
Cse viii-advanced-computer-architectures-06cs81-solution
Cse viii-advanced-computer-architectures-06cs81-solutionCse viii-advanced-computer-architectures-06cs81-solution
Cse viii-advanced-computer-architectures-06cs81-solution
 
Parallel Computing - Lec 6
Parallel Computing - Lec 6Parallel Computing - Lec 6
Parallel Computing - Lec 6
 
L-2 (Computer Performance).ppt
L-2 (Computer Performance).pptL-2 (Computer Performance).ppt
L-2 (Computer Performance).ppt
 
Basics of micro controllers for biginners
Basics of  micro controllers for biginnersBasics of  micro controllers for biginners
Basics of micro controllers for biginners
 
BAXTER phase 1b
BAXTER phase 1bBAXTER phase 1b
BAXTER phase 1b
 
Project Life Cycle and Effort Estimation
Project Life Cycle andEffort EstimationProject Life Cycle andEffort Estimation
Project Life Cycle and Effort Estimation
 
Algorithmic problem sloving
Algorithmic problem slovingAlgorithmic problem sloving
Algorithmic problem sloving
 
Embedded _c_
Embedded  _c_Embedded  _c_
Embedded _c_
 
OpenPOWER Webinar
OpenPOWER Webinar OpenPOWER Webinar
OpenPOWER Webinar
 
Practical C++ Generative Programming
Practical C++ Generative ProgrammingPractical C++ Generative Programming
Practical C++ Generative Programming
 
Cocomo model
Cocomo modelCocomo model
Cocomo model
 
ScilabTEC 2015 - Noesis Solutions
ScilabTEC 2015 - Noesis SolutionsScilabTEC 2015 - Noesis Solutions
ScilabTEC 2015 - Noesis Solutions
 
Introduction to embedded computing and arm processors
Introduction to embedded computing and arm processorsIntroduction to embedded computing and arm processors
Introduction to embedded computing and arm processors
 
JIT Compiler
JIT CompilerJIT Compiler
JIT Compiler
 
COCOMO 1 Model ppt AR-1.pdf
COCOMO 1 Model  ppt AR-1.pdfCOCOMO 1 Model  ppt AR-1.pdf
COCOMO 1 Model ppt AR-1.pdf
 
pipeline in computer architecture design
pipeline in computer architecture  designpipeline in computer architecture  design
pipeline in computer architecture design
 
C- language Lecture 4
C- language Lecture 4C- language Lecture 4
C- language Lecture 4
 

More from Akhil Kaushik

Symbol Table, Error Handler & Code Generation
Symbol Table, Error Handler & Code GenerationSymbol Table, Error Handler & Code Generation
Symbol Table, Error Handler & Code GenerationAkhil Kaushik
 
Parsing in Compiler Design
Parsing in Compiler DesignParsing in Compiler Design
Parsing in Compiler DesignAkhil Kaushik
 
Context Free Grammar
Context Free GrammarContext Free Grammar
Context Free GrammarAkhil Kaushik
 
Error Detection & Recovery
Error Detection & RecoveryError Detection & Recovery
Error Detection & RecoveryAkhil Kaushik
 
Lexical Analyzer Implementation
Lexical Analyzer ImplementationLexical Analyzer Implementation
Lexical Analyzer ImplementationAkhil Kaushik
 
Lexical Analysis - Compiler Design
Lexical Analysis - Compiler DesignLexical Analysis - Compiler Design
Lexical Analysis - Compiler DesignAkhil Kaushik
 
File Handling Python
File Handling PythonFile Handling Python
File Handling PythonAkhil Kaushik
 
Algorithms & Complexity Calculation
Algorithms & Complexity CalculationAlgorithms & Complexity Calculation
Algorithms & Complexity CalculationAkhil Kaushik
 
Intro to Data Structure & Algorithms
Intro to Data Structure & AlgorithmsIntro to Data Structure & Algorithms
Intro to Data Structure & AlgorithmsAkhil Kaushik
 
Decision Making & Loops
Decision Making & LoopsDecision Making & Loops
Decision Making & LoopsAkhil Kaushik
 
Basic programs in Python
Basic programs in PythonBasic programs in Python
Basic programs in PythonAkhil Kaushik
 
Introduction to Python Programming
Introduction to Python ProgrammingIntroduction to Python Programming
Introduction to Python ProgrammingAkhil Kaushik
 
Compiler Design Basics
Compiler Design BasicsCompiler Design Basics
Compiler Design BasicsAkhil Kaushik
 
Bootstrapping in Compiler
Bootstrapping in CompilerBootstrapping in Compiler
Bootstrapping in CompilerAkhil Kaushik
 
Compiler construction tools
Compiler construction toolsCompiler construction tools
Compiler construction toolsAkhil Kaushik
 

More from Akhil Kaushik (20)

Symbol Table, Error Handler & Code Generation
Symbol Table, Error Handler & Code GenerationSymbol Table, Error Handler & Code Generation
Symbol Table, Error Handler & Code Generation
 
Parsing in Compiler Design
Parsing in Compiler DesignParsing in Compiler Design
Parsing in Compiler Design
 
Context Free Grammar
Context Free GrammarContext Free Grammar
Context Free Grammar
 
Error Detection & Recovery
Error Detection & RecoveryError Detection & Recovery
Error Detection & Recovery
 
Symbol Table
Symbol TableSymbol Table
Symbol Table
 
Lexical Analyzer Implementation
Lexical Analyzer ImplementationLexical Analyzer Implementation
Lexical Analyzer Implementation
 
NFA & DFA
NFA & DFANFA & DFA
NFA & DFA
 
Lexical Analysis - Compiler Design
Lexical Analysis - Compiler DesignLexical Analysis - Compiler Design
Lexical Analysis - Compiler Design
 
File Handling Python
File Handling PythonFile Handling Python
File Handling Python
 
Regular Expressions
Regular ExpressionsRegular Expressions
Regular Expressions
 
Algorithms & Complexity Calculation
Algorithms & Complexity CalculationAlgorithms & Complexity Calculation
Algorithms & Complexity Calculation
 
Intro to Data Structure & Algorithms
Intro to Data Structure & AlgorithmsIntro to Data Structure & Algorithms
Intro to Data Structure & Algorithms
 
Decision Making & Loops
Decision Making & LoopsDecision Making & Loops
Decision Making & Loops
 
Basic programs in Python
Basic programs in PythonBasic programs in Python
Basic programs in Python
 
Python Data-Types
Python Data-TypesPython Data-Types
Python Data-Types
 
Introduction to Python Programming
Introduction to Python ProgrammingIntroduction to Python Programming
Introduction to Python Programming
 
Compiler Design Basics
Compiler Design BasicsCompiler Design Basics
Compiler Design Basics
 
Bootstrapping in Compiler
Bootstrapping in CompilerBootstrapping in Compiler
Bootstrapping in Compiler
 
Compiler construction tools
Compiler construction toolsCompiler construction tools
Compiler construction tools
 
Phases of compiler
Phases of compilerPhases of compiler
Phases of compiler
 

Recently uploaded

Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfSanaAli374401
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.MateoGardella
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterMateoGardella
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 

Recently uploaded (20)

Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 

Code Optimization

  • 1. Akhil Kaushik Asstt. Prof., CE Deptt., TIT Bhiwani Code Optimization
  • 2. Optimization • In computing, optimization is the process of modifying a system to make some aspect of it work more efficiently or use fewer resources. • Ex: a computer program may be optimized so that it executes more rapidly or is capable of operating with less memory storage or other resources, or draw less power. • The system may be a single computer program, a collection of computers or even an entire network such as the internet.
  • 3. Code Optimization • Optimization is a program transformation technique, which tries to improve the code that consume less resources (i.e. CPU, Memory) and deliver high speed. • In optimization, high-level general programming constructs are replaced by very efficient low-level programming codes. • Usually done at the end of the development stage since it reduces readability & adds code that is used to improve the performance.
  • 4. Code Optimization Optimized code’s features:- • Executes faster • Code size get reduced • Efficient memory usage • Yielding better performance • Reduces the time and space complexity
  • 5. Code Optimization Optimizations are classified into two categories: • Machine independent optimizations - improve target code without taking properties of target machine into consideration. • Machine dependent optimization - improve target code by checking properties of target machine.
  • 6. Code Optimization Criteria for Optimization:- • An optimization must preserve the meaning of a program: -Cannot change the output produced for any input. -Can not introduce an error. • Optimization should, on average, speed up programs. • Optimization should itself be fast and should not delay the overall compiling process.
  • 7. Code Optimization Optimization can occur at several levels: 1.Design level:- At the highest level, the design may be optimized to make best use of the available resources. – The implementation of this design will benefit from the use of suitable efficient algorithms and the implementation of these algorithms will benefit from writing good quality code. 2.Compile level:- Use of an optimizing compiler tends to ensure that the executable program is optimized at least as much as the compiler can predict
  • 8. Code Optimization Improvements can be made at various phases:- • Source Code: Algorithm's transformation can produce spectacular improvements • Intermediate Code: Compiler can improve loops, procedure calls and address calculations. -Typically only optimizing compilers include this phase • Target Code: Compilers can use registers efficiently
  • 9. Code Optimization • Main techniques of Optimization:-
  • 10. Code Optimization There are primarily 3 types of optimizations:- (1) Local optimization - Apply to a basic block in isolation (2) Global optimization - Apply across basic blocks (3) Loop optimization – Apply on loops Another significant optimization can be:- (4) peep-hole optimization - Apply across boundaries
  • 11. Code Optimization • Optimization performed within a basic block. • This is simplest form of optimizations • No need to analyze the whole procedure body. • Just analyze the basic blocks of the procedure. • The local optimization techniques include: – Constant Folding – Constant Propagation – Algebraic Simplification – Operator Strength Reduction – Dead Code Elimination
  • 12. Local Code Optimization 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. • Ex: Circumference of circle = (22/7) x Diameter • Replace (22/7) with 3.14 during compile time and save the execution time.
  • 13. Local Code Optimization 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. • Ex: pi = 3.14, radius = 10 • Area of circle = pi x radius x radius • Substitutes constants value during compile time and saves the execution time.
  • 14. Local Code Optimization C) Algebraic Simplification- • Use algebraic properties to simplify expressions • Some expressions can be simplified by replacing them with an equivalent expression that is more efficient. • Ex:
  • 15. Code Optimization D) 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
  • 16. Code Optimization E) 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.
  • 17. Code Optimization F) Common sub-expressions elimination: • Eliminates the redundant expressions and avoids their computation again and again.
  • 18. Global Code Optimization • Optimization across basic blocks within a procedure /function • Could be restricted to a smaller scope, Example: a loop • Data-flow analysis is done to perform optimization across basic blocks • Each basic block is a node in the flow graph of the program. • These optimizations can be extended to an entire control - flow graph • Most of compiler implement global optimizations with well founded theory and practical gains
  • 19. Peephole Optimization • Optimization technique that operates on the one or few instructions at a time. • Performs machine dependent improvements • Peeps into a single or sequence of two to three instructions (peephole) and replaces it by most efficient alternative (shorter or faster) instructions. • Peephole is a small moving window on the target systems
  • 20. Peephole Optimization Characteristics of peep-hole optimizations:- • Redundant-instruction (loads and stores)elimination • Flow-of-control optimizations - - Elimination of multiple jumps (i.e. goto statements) • Elimination of unreachable code • Algebraic simplifications • Reducing operator strength • Use of machine idioms
  • 21. Loop Optimization • Loop optimization plays an important role in improving the performance of the source code by reducing overheads associated with executing loops. • Loop Optimization can be done by removing: –Loop invariant –Induction variables
  • 22. Loop Optimization • Removal of Loop Invariant data:-
  • 24. Akhil Kaushik akhilkaushik05@gmail.com 9416910303 CONTACT ME AT: Akhil Kaushik akhilkaushik05@gmail.com 9416910303 THANK YOU !!!