SlideShare a Scribd company logo
3
Most read
5
Most read
10
Most read
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 (20)

PPTX
Specification-of-tokens
Dattatray Gandhmal
 
PPTX
Code generation
Aparna Nayak
 
PDF
Object oriented programming c++
Ankur Pandey
 
PPTX
Unit 4 sp macro
Deepmala Sharma
 
PPTX
Phases of compiler
Karan Deopura
 
PPT
1.Role lexical Analyzer
Radhakrishnan Chinnusamy
 
PPTX
Loop optimization
Vivek Gandhi
 
PPTX
Peephole Optimization
United International University
 
PPTX
Input-Buffering
Dattatray Gandhmal
 
PPTX
Recognition-of-tokens
Dattatray Gandhmal
 
PPTX
Interface specification
maliksiddique1
 
PPTX
System calls
Bernard Senam
 
PPT
Type Checking(Compiler Design) #ShareThisIfYouLike
United International University
 
PPTX
Macro Processor
Saranya1702
 
PPT
Symbol table management and error handling in compiler design
Swati Chauhan
 
PPTX
Phases of compiler
Akhil Kaushik
 
PPTX
Relationship Among Token, Lexeme & Pattern
Bharat Rathore
 
PPTX
Unit iv(simple code generator)
Kalaimathi Vijayakumar
 
PPTX
Principal source of optimization in compiler design
Rajkumar R
 
PPTX
Types of Parser
SomnathMore3
 
Specification-of-tokens
Dattatray Gandhmal
 
Code generation
Aparna Nayak
 
Object oriented programming c++
Ankur Pandey
 
Unit 4 sp macro
Deepmala Sharma
 
Phases of compiler
Karan Deopura
 
1.Role lexical Analyzer
Radhakrishnan Chinnusamy
 
Loop optimization
Vivek Gandhi
 
Peephole Optimization
United International University
 
Input-Buffering
Dattatray Gandhmal
 
Recognition-of-tokens
Dattatray Gandhmal
 
Interface specification
maliksiddique1
 
System calls
Bernard Senam
 
Type Checking(Compiler Design) #ShareThisIfYouLike
United International University
 
Macro Processor
Saranya1702
 
Symbol table management and error handling in compiler design
Swati Chauhan
 
Phases of compiler
Akhil Kaushik
 
Relationship Among Token, Lexeme & Pattern
Bharat Rathore
 
Unit iv(simple code generator)
Kalaimathi Vijayakumar
 
Principal source of optimization in compiler design
Rajkumar R
 
Types of Parser
SomnathMore3
 

Similar to Code Optimization (20)

PPTX
Code optimization
veena venugopal
 
PPTX
Code optmize.pptx which is related to coding
vamami6395
 
PPTX
Principal Sources of Optimization in compiler design
LogsAk
 
PPT
basics of optimizations presentation s
AnkitKumarSharma26
 
PPT
Code Optimization Lec#7.ppt Code Optimizer
zeeshanmubeen1
 
PDF
Optimization in Programming languages
Ankit Pandey
 
PDF
Code Optimizatoion
rajuvermadsvv
 
PPTX
Code optimization
Sameenafathima4
 
PPTX
Downlaod Wise Registry Cleaner Pro Crack
tomcross837
 
PPTX
Autodesk CFD Ultimate Crack Latest Version
losbeten
 
PPTX
Download RarmaRadio Pro Crack Latest [2025]
wreeen464
 
PPTX
Code_Optimization_Compiler_Design software .pptx
AmishaKumari97
 
PPTX
NCH VideoPad Pro Cracked Version Download
jasperjhin57
 
PPTX
Download Artweaver Plus Cracked Version Free
adomalex314
 
PPTX
Latest TreeSize Professional 9 Crack Download
bentenkhan340
 
PPTX
PowerDirector Activated Full Tested Download
tomveren
 
PPTX
Code optimization
Pradip Bhattarai
 
PPTX
Code Optimization In Code Generator In Compiler Design Subject.pptx
22r11a05j2
 
PPTX
complier design unit 5 for helping students
aniketsugandhi1
 
PPTX
UNIT V - Compiler Design notes power point presentation
KowsalyaG17
 
Code optimization
veena venugopal
 
Code optmize.pptx which is related to coding
vamami6395
 
Principal Sources of Optimization in compiler design
LogsAk
 
basics of optimizations presentation s
AnkitKumarSharma26
 
Code Optimization Lec#7.ppt Code Optimizer
zeeshanmubeen1
 
Optimization in Programming languages
Ankit Pandey
 
Code Optimizatoion
rajuvermadsvv
 
Code optimization
Sameenafathima4
 
Downlaod Wise Registry Cleaner Pro Crack
tomcross837
 
Autodesk CFD Ultimate Crack Latest Version
losbeten
 
Download RarmaRadio Pro Crack Latest [2025]
wreeen464
 
Code_Optimization_Compiler_Design software .pptx
AmishaKumari97
 
NCH VideoPad Pro Cracked Version Download
jasperjhin57
 
Download Artweaver Plus Cracked Version Free
adomalex314
 
Latest TreeSize Professional 9 Crack Download
bentenkhan340
 
PowerDirector Activated Full Tested Download
tomveren
 
Code optimization
Pradip Bhattarai
 
Code Optimization In Code Generator In Compiler Design Subject.pptx
22r11a05j2
 
complier design unit 5 for helping students
aniketsugandhi1
 
UNIT V - Compiler Design notes power point presentation
KowsalyaG17
 
Ad

More from Akhil Kaushik (20)

PPT
Symbol Table, Error Handler & Code Generation
Akhil Kaushik
 
PPTX
Parsing in Compiler Design
Akhil Kaushik
 
PPTX
Context Free Grammar
Akhil Kaushik
 
PPTX
Error Detection & Recovery
Akhil Kaushik
 
PPTX
Symbol Table
Akhil Kaushik
 
PPTX
Lexical Analyzer Implementation
Akhil Kaushik
 
PPTX
NFA & DFA
Akhil Kaushik
 
PPTX
Lexical Analysis - Compiler Design
Akhil Kaushik
 
PPTX
File Handling Python
Akhil Kaushik
 
PPTX
Regular Expressions
Akhil Kaushik
 
PPTX
Algorithms & Complexity Calculation
Akhil Kaushik
 
PPTX
Intro to Data Structure & Algorithms
Akhil Kaushik
 
PPTX
Decision Making & Loops
Akhil Kaushik
 
PPTX
Basic programs in Python
Akhil Kaushik
 
PPTX
Python Data-Types
Akhil Kaushik
 
PPTX
Introduction to Python Programming
Akhil Kaushik
 
PPT
Compiler Design Basics
Akhil Kaushik
 
PPTX
Bootstrapping in Compiler
Akhil Kaushik
 
PPTX
Compiler construction tools
Akhil Kaushik
 
PPTX
Introduction to Compilers
Akhil Kaushik
 
Symbol Table, Error Handler & Code Generation
Akhil Kaushik
 
Parsing in Compiler Design
Akhil Kaushik
 
Context Free Grammar
Akhil Kaushik
 
Error Detection & Recovery
Akhil Kaushik
 
Symbol Table
Akhil Kaushik
 
Lexical Analyzer Implementation
Akhil Kaushik
 
NFA & DFA
Akhil Kaushik
 
Lexical Analysis - Compiler Design
Akhil Kaushik
 
File Handling Python
Akhil Kaushik
 
Regular Expressions
Akhil Kaushik
 
Algorithms & Complexity Calculation
Akhil Kaushik
 
Intro to Data Structure & Algorithms
Akhil Kaushik
 
Decision Making & Loops
Akhil Kaushik
 
Basic programs in Python
Akhil Kaushik
 
Python Data-Types
Akhil Kaushik
 
Introduction to Python Programming
Akhil Kaushik
 
Compiler Design Basics
Akhil Kaushik
 
Bootstrapping in Compiler
Akhil Kaushik
 
Compiler construction tools
Akhil Kaushik
 
Introduction to Compilers
Akhil Kaushik
 
Ad

Recently uploaded (20)

PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PPTX
Pyhton with Mysql to perform CRUD operations.pptx
Ramakrishna Reddy Bijjam
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
PPTX
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
PDF
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PPTX
Views on Education of Indian Thinkers Mahatma Gandhi.pptx
ShrutiMahanta1
 
PDF
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
PPTX
THE TAME BIRD AND THE FREE BIRD.pptxxxxx
MarcChristianNicolas
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PDF
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PPTX
grade 5 lesson ENGLISH 5_Q1_PPT_WEEK3.pptx
SireQuinn
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PPTX
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PDF
community health nursing question paper 2.pdf
Prince kumar
 
PDF
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
PDF
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
Pyhton with Mysql to perform CRUD operations.pptx
Ramakrishna Reddy Bijjam
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
Views on Education of Indian Thinkers Mahatma Gandhi.pptx
ShrutiMahanta1
 
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
THE TAME BIRD AND THE FREE BIRD.pptxxxxx
MarcChristianNicolas
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
grade 5 lesson ENGLISH 5_Q1_PPT_WEEK3.pptx
SireQuinn
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
Dimensions of Societal Planning in Commonism
StefanMz
 
community health nursing question paper 2.pdf
Prince kumar
 
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 

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:-