SlideShare a Scribd company logo
Code Optimization
Code optimization is a technique in which code of a program is transformed in such a way
that it uses fewer resources (instructions, memory) or it works efficiently.
Example: The following function return the sum of two numbers in C++.
int sum(int num1, int num2){
int sum;
sum = num1 +num2;
return sum;
}
The above code can be optimized in such a way that it can only takes one instruction and it
does not reserve memory for sum variable.
int sum(int num1, int num2){
return num1 + num2;
}
Code OptimizationTechniques
 Branch optimization
It transforms the code to optimize the branching statements.
Example: The code for finding greatest number between three numbers.
if(num1>num2){
if(num1>num3){
greatNum = num1;
}
else{
greatNum = num3;
}
else{
if(num2>num3){
greatNum = num2;
}
else{
greatNum = num3;
}
}
The above code can be optimized in such a way that it takes less branching statements.
if(num1>num2 && num1>num3)
greatNum = num1;
else if (num2>num1 && num2>num3)
greatNum = num2;
else
greatNum = num3;
 In-lining function
Convert the regular function into In-lining function if needed.
Example: The function that return the sum of two numbers.
int sum(int a, int b){
return a+b;
}
The above code can be optimized by converting it into In-lining.
#define sum(a, b)((a+b))
 Recursive function
Convert the regular function into In-lining function if possible.
Example: The function that return the factorial of number.
int factorial(int n){
int fact=1;
for(int i=1; i<=n; i++)
fact=fact*i;
return fact;
}
The above code can be optimized by converting it into recursive function.
int factorial(int n){
if(n==1)
return 1;
else
return n*factorial(n-1);
}

More Related Content

PPTX
Presentation(intermediate code generation)
PPTX
Three Address code
PPTX
Pa1 wednesday flow_chart
PDF
Fast coputation of Phi(x) inverse
PPTX
Ternary operator
PDF
Computer Science Engineering : Data structure & algorithm, THE GATE ACADEMY
PPTX
Three address code generation
PPT
Code Tuning
Presentation(intermediate code generation)
Three Address code
Pa1 wednesday flow_chart
Fast coputation of Phi(x) inverse
Ternary operator
Computer Science Engineering : Data structure & algorithm, THE GATE ACADEMY
Three address code generation
Code Tuning

Similar to Code Optimization in MIPS (20)

PDF
Code Optimizatoion
PPTX
Course work Questions and answers .pptx
PPTX
CPP Homework Help
PPTX
VCE Unit 01 (2).pptx
PPTX
design analysis of algorithmaa unit 1.pptx
PDF
Lesson 26. Optimization of 64-bit programs
PPT
Code Tuning
PDF
c++ referesher 1.pdf
PDF
Optimization in Programming languages
PPTX
Object Oriented Design and Programming Unit-04
PDF
complexity analysis.pdf
PPTX
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
PPTX
Introduction to Design and Analysis of Algorithms
PPTX
Unit i basic concepts of algorithms
PDF
Performance Analysis,Time complexity, Asymptotic Notations
PPTX
DESIGN AND ALGORITHM.pptx BCA BANGALORECITY UNIVERSITY
PPT
Fundamentals of the Analysis of Algorithm Efficiency
PDF
Taking r to its limits. 70+ tips
PPTX
C and C++ programming basics for Beginners.pptx
Code Optimizatoion
Course work Questions and answers .pptx
CPP Homework Help
VCE Unit 01 (2).pptx
design analysis of algorithmaa unit 1.pptx
Lesson 26. Optimization of 64-bit programs
Code Tuning
c++ referesher 1.pdf
Optimization in Programming languages
Object Oriented Design and Programming Unit-04
complexity analysis.pdf
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Introduction to Design and Analysis of Algorithms
Unit i basic concepts of algorithms
Performance Analysis,Time complexity, Asymptotic Notations
DESIGN AND ALGORITHM.pptx BCA BANGALORECITY UNIVERSITY
Fundamentals of the Analysis of Algorithm Efficiency
Taking r to its limits. 70+ tips
C and C++ programming basics for Beginners.pptx
Ad

Recently uploaded (20)

PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
PDF
LDMMIA Reiki Yoga Finals Review Spring Summer
PPTX
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
PDF
Indian roads congress 037 - 2012 Flexible pavement
PPTX
UNIT III MENTAL HEALTH NURSING ASSESSMENT
PPTX
Digestion and Absorption of Carbohydrates, Proteina and Fats
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PPTX
Lesson notes of climatology university.
PDF
RMMM.pdf make it easy to upload and study
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PDF
Hazard Identification & Risk Assessment .pdf
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
UV-Visible spectroscopy..pptx UV-Visible Spectroscopy – Electronic Transition...
PPTX
Cell Types and Its function , kingdom of life
PPTX
A powerpoint presentation on the Revised K-10 Science Shaping Paper
PPTX
Introduction to Building Materials
PDF
SOIL: Factor, Horizon, Process, Classification, Degradation, Conservation
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
Final Presentation General Medicine 03-08-2024.pptx
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
LDMMIA Reiki Yoga Finals Review Spring Summer
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
Indian roads congress 037 - 2012 Flexible pavement
UNIT III MENTAL HEALTH NURSING ASSESSMENT
Digestion and Absorption of Carbohydrates, Proteina and Fats
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Lesson notes of climatology university.
RMMM.pdf make it easy to upload and study
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
Hazard Identification & Risk Assessment .pdf
Chinmaya Tiranga quiz Grand Finale.pdf
UV-Visible spectroscopy..pptx UV-Visible Spectroscopy – Electronic Transition...
Cell Types and Its function , kingdom of life
A powerpoint presentation on the Revised K-10 Science Shaping Paper
Introduction to Building Materials
SOIL: Factor, Horizon, Process, Classification, Degradation, Conservation
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
Ad

Code Optimization in MIPS

  • 1. Code Optimization Code optimization is a technique in which code of a program is transformed in such a way that it uses fewer resources (instructions, memory) or it works efficiently. Example: The following function return the sum of two numbers in C++. int sum(int num1, int num2){ int sum; sum = num1 +num2; return sum; } The above code can be optimized in such a way that it can only takes one instruction and it does not reserve memory for sum variable. int sum(int num1, int num2){ return num1 + num2; } Code OptimizationTechniques  Branch optimization It transforms the code to optimize the branching statements. Example: The code for finding greatest number between three numbers. if(num1>num2){ if(num1>num3){ greatNum = num1; } else{ greatNum = num3; } else{ if(num2>num3){ greatNum = num2; }
  • 2. else{ greatNum = num3; } } The above code can be optimized in such a way that it takes less branching statements. if(num1>num2 && num1>num3) greatNum = num1; else if (num2>num1 && num2>num3) greatNum = num2; else greatNum = num3;  In-lining function Convert the regular function into In-lining function if needed. Example: The function that return the sum of two numbers. int sum(int a, int b){ return a+b; } The above code can be optimized by converting it into In-lining. #define sum(a, b)((a+b))  Recursive function Convert the regular function into In-lining function if possible. Example: The function that return the factorial of number. int factorial(int n){ int fact=1; for(int i=1; i<=n; i++) fact=fact*i; return fact; } The above code can be optimized by converting it into recursive function. int factorial(int n){ if(n==1) return 1; else return n*factorial(n-1); }