SlideShare a Scribd company logo
1 of 26
Download to read offline
CODE OPTIMIZATION
IN
PROGRAMMING LANGUAGE
Ankit Pandey
Code Optimization
 Why
 To derive the best possible performance from the given
resources .
 To balance Speed and Memory Tradeoffs according to
requirements.
 More emphasis is on writing efficient code (Smart code)
 Faster execution
 Efficient memory usages
 Yielding better performance
 What
 Optimization is the process of transforming a piece of code
to make it more efficient.
 It does not change the meaning of program.
Code Optimization
 Code Optimization Techniques
 Constant propagation
 Constant folding
 Algebraic simplification, strength reduction
 Copy propagation
Code Optimization
 Techniques for Speed optimization
 Constant propagation
 Constant folding
 Algebraic simplification, strength reduction
 Copy propagation
 Common subexpression elimination
 Unreacheable code elimination
 Dead code elimination
 Loop Optimization
 Function related
Code Optimization
 Techniques for Memory optimization
 Reduce Padding
 Local Vs Global Variables
Code Optimization Techniques
 Constant Propagation
 If the value of a variable is a constant, then replace the
variable by the constant
 E.g.
N := 10; C := 2;
for (i:=0; i<N; i++) { s := s + i*C; }
 for (i:=0; i<10; i++) { s := s + i*2; }
 Constant Folding
 Simplifying constant expressions as much as possible
 E.g.
static final int a = 2; int b = 30 * a;
// folding would create
int b = 60;
Code Optimization Techniques
 Algebraic simplification
 More general form of constant folding, e.g.,
 x + 0  x x – 0  x
 x * 1  x x / 1  x
 x * 0  0
 Repeatedly apply the rules
 (y * 1 + 0) / 1  y
 Strength reduction
 Replace expensive operations
 E.g., x := x * 8  x := x << 3
Code Optimization Techniques
 Code Motion (Out of loops)
 A modification that decreases the amount of code in a loop.
 Invariant expressions should be executed only once.
 E.g.
for (int i = 0; i < x.length; i++)
x[i] *= Math.PI * Math.cos(y);
Code motion will result in the equivalent of
double picosy = Math.PI * Math.cos(y);
for (int i = 0; i < x.length; i++)
x[i] *= picosy;
Code Optimization Techniques
 Copy propagation
 Extension of constant propagation
 After y is assigned to x, use y to replace x till x is
assigned again
 Example
x := y;  s := y * f(y)
s := x * f(x)
 Reduce the copying
 If y is reassigned in between, then this action cannot be
performed
Code Optimization Techniques
 Common sub expression elimination
 Searches for instances of identical expression,
replacing them with a single variable holding the
computed value (if possible)
 Example:
a := b + c a := b + c
c := b + c  c := a
d := b + c d := b + c
 Example in array index calculations
 c[i+1] := a[i+1] + b[i+1]
 During address computation, i+1 should be reused
 Not visible in high level code, but in intermediate code
Code Optimization Techniques
 Unreacheable code elimination
 Construct the control flow graph
 Unreachable code block will not have an incoming edge
 Dead code elimination
 Ineffective statements
 x := y + 1 (immediately redefined, eliminate!)
 y := 5  y := 5
 x := 2 * z x := 2 * z
 A variable is dead if it is never used after last definition
 Eliminate assignments to dead variables
 Need to do data flow analysis to find dead variables
Code Optimization Techniques
 Function inlining
 Replace a function call with the body of the function.
 Program will spend less time in the function call and
return parts, etc.
 It has also some disadvantages – Increase program size
and break
encapsulation.
 Function cloning
 Create specialized code for a function for different
calling parameters
Code Optimization Techniques
 Loop optimization
 Is the process of the increasing execution speed and
reducing the overheads associated of loops.
 Consumes 90% of the execution time
 a larger payoff to optimize the code within a loop
 Techniques
 Loop invariant detection and code motion
 Induction variable elimination
 Strength reduction in loops
 Loop unrolling
 Loop peeling
 Loop fusion
Code Optimization Techniques
 Loop invariant detection and code motion
 If the result of a statement or expression does not change within
a loop, and it has no external side-effect
 Computation can be moved to the outside of the loop
 Example
for (i=0; i<n; i++) a[i] := a[i] + x/y;
 for (i=0; i<n; i++) { c := x/y; a[i] := a[i] + c; } // three address code
 c := x/y; for (i=0; i<n; i++) a[i] := a[i] + c;
 Induction variable elimination
 If there are multiple induction variables in a loop, can eliminate
the ones which are used only in the test condition
 Example
s := 0; for (i=0; i<n; i++) { s := s + 4; }
 s := 0; while (s < 4*n) { s := s + 4; }
Code Optimization Techniques
 Strength reduction in loops
 Example
s := 0; for (i=0; i<n; i++) { v := 4 * i; s := s + v; )
 s := 0; for (i=0; i<n; i++) { v := v + 4; s := s + v; )
 Loop Termination
 The loop termination condition can cause significant
overhead if written without caution.
 Should always write count-down-to-zero loops and use
simple termination conditions.
 Example
int fact1_func (int n) { int i, fact = 1; for (i = 1; i <= n; i++)
 int fact1_func (int n) { int i, fact = 1; for (i = 1; i <= n; i++)
Code Optimization Techniques
 Loop unrolling
 Execute loop body multiple times at each iteration
 Get rid of the conditional branches, if possible
 Allow optimization to cross multiple iterations of the loop
 Especially for parallel instruction execution
 Space time tradeoff
 Increase in code size, reduce some instruction
 Example
for(i=0; i<3; i++) { something(i); }
After loop unrolling –
for(i=0; i<3; i++){
something(0); something(1); something(2);}
 Loop peeling
 Similar to unrolling
 But unroll the first and/or last iterations
Code Optimization Techniques
 Loop fusion
 Some adjacent loops can be fused into one loop to
reduce loop overhead and improve run-time
performance.
 Example
for i=1 to N do
A[i] = B[i] + 1
endfor
for i=1 to N do
C[i] = A[i] / 2
endfor
for i=1 to N do
D[i] = 1 / C[i+1]
endforBefore Loop Fusion
for i=1 to N do
A[i] = B[i] + 1
C[i] = A[i] / 2
D[i] = 1 / C[i+1]
endfor
Code Optimization Techniques
Memory based Optimization
 Reduce Padding
 Declare local variables based on their size. Prefer to
declare larger to smaller data type variables.
 E.g.
struct
{ char x;
int y; }
Here size of struct is 4 bytes !!!!!
 Points to be remember while using Register variable –
Code Optimization Techniques
 Let us discuss one more example –
/* sizeof = 64 bytes */ /*sizeof = 48 bytes */
struct foo sturct foo
{ float a; { double b;
double b; double d;
float c; long f;
double d; long h;
short e; float c;
long f; float a;
short g; int j;
long h; int l;
char i; short e;
int j; short g;
char k; char k;
int l; }; char I;};
Code Optimization Techniques
 Local Vs Global Variables
 Local variables are stored in registers.
 Keep the number of local variables low to allow them to be fit in the
available registers and avoid stack usage.
 Global Variables are stored in RAM.
 Manipulating and accessing local variables is less costly
as it uses registers compared to accessing from memory.
 Avoid referring to global or static variables inside the
tightest loops, instead use local variables.
 Declare local variables in the inner most scope.
Code Optimization Techniques
 Locality of Reference
 Local variables are stored in registers.
 Keep the number of local variables low to allow them to be fit in the
available registers and avoid stack usage.
 Global Variables are stored in RAM.
 Manipulating and accessing local variables is less costly
as it uses registers compared to accessing from memory.
 Avoid referring to global or static variables inside the
tightest loops, instead use local variables.
 Declare local variables in the inner most scope.
Code Optimization tricks in „C‟
Language
 Usages of Register Variables
 Use Register variables whenever possible.
 Accessed from register rather from memory, so accessing
is faster.
 E.g.
register float val;
register double dval;
register int ival;
 Points to be remember while using Register variable -
 Only declare those variables as register which are heavily used
 We can not take the address of register variable , so never try this.
&ival would fail.
Code Optimization tricks in „C‟
Language
 String Operations
 Most of the C library str* functions operate in time
proportional to the length(s) of the string(s) they are given.
 Avoid calling strlen( ) during a loop involving the string itself.
 strcat( ) will scan the full (current) length of the string on
each call. If you've been keeping track of the length
anyway , you can index directly to the end of the string and
strcpy to there.
 Try to avoid stecmp( ) as much as possible. If is not then try
to use it smartly!!!
 Try to avoid empty string test using strlen() function.
Avoid strlen(s) == 0 use *s == „0‟
Avoid strcpy(s, “ ”) use *s = „0‟
Code Optimization tricks in „C‟
Language
 Aliasing – It do optimization , try it!!!!
 Consider the following:
void func1( int *data ) {
int i;
for(i=0; i<10; i++)
{ somefunc2( *data, i); }
}
 Optimized code after aliasing :
void func1( int *data ) {
int i; int localdata;
localdata = *data;
for(i=0; i<10; i++) {
somefunc2( localdata, i); } }
Code Optimization tricks in „C‟
Language
 Miscellaneous
 Use unsigned int instead of int if we know the value will never be
negative.
 Avoid unnecessary division.
For eg - (a / b) > c can be rewritten as a > (c * b)
 Use shift operations >> and << instead of integer multiplication and
division, where possible.
 Try to use Switch in place of nested if…elseif…elseif…else
 Prefer Integer comparison.
 Avoid calculations in loop and try to use simplify expression.
 Try to minimize usages of global variables. They are never allocated to
registers.
 use the prefix operator (++obj) instead of the postfix operator (obj++).
Code Optimization tricks in „C‟
Language
 Miscellaneous Continue…..
 Use unsigned int instead of int if we know the value will
never be negative.
 Avoid unnecessary division.

More Related Content

What's hot

Compiler optimization
Compiler optimizationCompiler optimization
Compiler optimizationZongYing Lyu
 
Compiler optimization techniques
Compiler optimization techniquesCompiler optimization techniques
Compiler optimization techniquesHardik Devani
 
Compiler optimization
Compiler optimizationCompiler optimization
Compiler optimizationliu_ming50
 
Principle source of optimazation
Principle source of optimazationPrinciple source of optimazation
Principle source of optimazationSiva Sathya
 
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
 
Arm developement
Arm developementArm developement
Arm developementhirokiht
 
Code optimisation presnted
Code optimisation presntedCode optimisation presnted
Code optimisation presntedbhavanatmithun
 
Function Overloading,Inline Function and Recursion in C++ By Faisal Shahzad
Function Overloading,Inline Function and Recursion in C++ By Faisal ShahzadFunction Overloading,Inline Function and Recursion in C++ By Faisal Shahzad
Function Overloading,Inline Function and Recursion in C++ By Faisal ShahzadFaisal Shehzad
 
Compiler optimizations based on call-graph flattening
Compiler optimizations based on call-graph flatteningCompiler optimizations based on call-graph flattening
Compiler optimizations based on call-graph flatteningCAFxX
 
Compiler optimization
Compiler optimizationCompiler optimization
Compiler optimizationKarthik Vivek
 
structured programming
structured programmingstructured programming
structured programmingAhmad54321
 
Basic information of function in cpu
Basic information of function in cpuBasic information of function in cpu
Basic information of function in cpuDhaval Jalalpara
 
Inline function in C++
Inline function in C++Inline function in C++
Inline function in C++Learn By Watch
 
Inline function(oops)
Inline function(oops)Inline function(oops)
Inline function(oops)Jay Patel
 

What's hot (20)

Code optimization
Code optimizationCode optimization
Code optimization
 
Compiler optimization
Compiler optimizationCompiler optimization
Compiler optimization
 
Compiler Design- Machine Independent Optimizations
Compiler Design- Machine Independent OptimizationsCompiler Design- Machine Independent Optimizations
Compiler Design- Machine Independent Optimizations
 
Compiler optimization techniques
Compiler optimization techniquesCompiler optimization techniques
Compiler optimization techniques
 
Compiler optimization
Compiler optimizationCompiler optimization
Compiler optimization
 
Principle source of optimazation
Principle source of optimazationPrinciple source of optimazation
Principle source of optimazation
 
Peephole Optimization
Peephole OptimizationPeephole Optimization
Peephole Optimization
 
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
 
Arm developement
Arm developementArm developement
Arm developement
 
Code optimisation presnted
Code optimisation presntedCode optimisation presnted
Code optimisation presnted
 
Function Overloading,Inline Function and Recursion in C++ By Faisal Shahzad
Function Overloading,Inline Function and Recursion in C++ By Faisal ShahzadFunction Overloading,Inline Function and Recursion in C++ By Faisal Shahzad
Function Overloading,Inline Function and Recursion in C++ By Faisal Shahzad
 
Embedded C - Optimization techniques
Embedded C - Optimization techniquesEmbedded C - Optimization techniques
Embedded C - Optimization techniques
 
Compiler optimizations based on call-graph flattening
Compiler optimizations based on call-graph flatteningCompiler optimizations based on call-graph flattening
Compiler optimizations based on call-graph flattening
 
Inline functions
Inline functionsInline functions
Inline functions
 
Compiler optimization
Compiler optimizationCompiler optimization
Compiler optimization
 
structured programming
structured programmingstructured programming
structured programming
 
Basic information of function in cpu
Basic information of function in cpuBasic information of function in cpu
Basic information of function in cpu
 
Unit 3 part2
Unit 3 part2Unit 3 part2
Unit 3 part2
 
Inline function in C++
Inline function in C++Inline function in C++
Inline function in C++
 
Inline function(oops)
Inline function(oops)Inline function(oops)
Inline function(oops)
 

Similar to Optimization in Programming languages

Compiler presention
Compiler presentionCompiler presention
Compiler presentionFaria Priya
 
Code Tuning
Code TuningCode Tuning
Code Tuningbgtraghu
 
CODE TUNINGtertertertrtryryryryrtytrytrtry
CODE TUNINGtertertertrtryryryryrtytrytrtryCODE TUNINGtertertertrtryryryryrtytrytrtry
CODE TUNINGtertertertrtryryryryrtytrytrtrykapib57390
 
Introduction to code optimization by dipankar
Introduction to code optimization by dipankarIntroduction to code optimization by dipankar
Introduction to code optimization by dipankarDipankar Nalui
 
Improving Code Quality Through Effective Review Process
Improving Code Quality Through Effective  Review ProcessImproving Code Quality Through Effective  Review Process
Improving Code Quality Through Effective Review ProcessDr. Syed Hassan Amin
 
Inline function
Inline functionInline function
Inline functionTech_MX
 
EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5PRADEEP
 
Object oriented programming system with C++
Object oriented programming system with C++Object oriented programming system with C++
Object oriented programming system with C++msharshitha03s
 
Actor Concurrency
Actor ConcurrencyActor Concurrency
Actor ConcurrencyAlex Miller
 
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
 
Pragmatic Optimization in Modern Programming - Demystifying the Compiler
Pragmatic Optimization in Modern Programming - Demystifying the CompilerPragmatic Optimization in Modern Programming - Demystifying the Compiler
Pragmatic Optimization in Modern Programming - Demystifying the CompilerMarina Kolpakova
 
Fast Insights to Optimized Vectorization and Memory Using Cache-aware Rooflin...
Fast Insights to Optimized Vectorization and Memory Using Cache-aware Rooflin...Fast Insights to Optimized Vectorization and Memory Using Cache-aware Rooflin...
Fast Insights to Optimized Vectorization and Memory Using Cache-aware Rooflin...Intel® Software
 

Similar to Optimization in Programming languages (20)

Code Tuning
Code TuningCode Tuning
Code Tuning
 
Compiler presention
Compiler presentionCompiler presention
Compiler presention
 
Code Tuning
Code TuningCode Tuning
Code Tuning
 
Code Optimizatoion
Code OptimizatoionCode Optimizatoion
Code Optimizatoion
 
CODE TUNINGtertertertrtryryryryrtytrytrtry
CODE TUNINGtertertertrtryryryryrtytrytrtryCODE TUNINGtertertertrtryryryryrtytrytrtry
CODE TUNINGtertertertrtryryryryrtytrytrtry
 
Introduction to code optimization by dipankar
Introduction to code optimization by dipankarIntroduction to code optimization by dipankar
Introduction to code optimization by dipankar
 
Improving Code Quality Through Effective Review Process
Improving Code Quality Through Effective  Review ProcessImproving Code Quality Through Effective  Review Process
Improving Code Quality Through Effective Review Process
 
Inline function
Inline functionInline function
Inline function
 
EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5
 
Object oriented programming system with C++
Object oriented programming system with C++Object oriented programming system with C++
Object oriented programming system with C++
 
Matopt
MatoptMatopt
Matopt
 
Code optimization
Code optimizationCode optimization
Code optimization
 
C Programming Homework Help
C Programming Homework HelpC Programming Homework Help
C Programming Homework Help
 
Actor Concurrency
Actor ConcurrencyActor Concurrency
Actor Concurrency
 
Code optimization
Code optimizationCode optimization
Code optimization
 
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
 
Pragmatic Optimization in Modern Programming - Demystifying the Compiler
Pragmatic Optimization in Modern Programming - Demystifying the CompilerPragmatic Optimization in Modern Programming - Demystifying the Compiler
Pragmatic Optimization in Modern Programming - Demystifying the Compiler
 
Code optimization
Code optimization Code optimization
Code optimization
 
Code optimization
Code optimization Code optimization
Code optimization
 
Fast Insights to Optimized Vectorization and Memory Using Cache-aware Rooflin...
Fast Insights to Optimized Vectorization and Memory Using Cache-aware Rooflin...Fast Insights to Optimized Vectorization and Memory Using Cache-aware Rooflin...
Fast Insights to Optimized Vectorization and Memory Using Cache-aware Rooflin...
 

Recently uploaded

Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceanilsa9823
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 

Recently uploaded (20)

Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 

Optimization in Programming languages

  • 2. Code Optimization  Why  To derive the best possible performance from the given resources .  To balance Speed and Memory Tradeoffs according to requirements.  More emphasis is on writing efficient code (Smart code)  Faster execution  Efficient memory usages  Yielding better performance  What  Optimization is the process of transforming a piece of code to make it more efficient.  It does not change the meaning of program.
  • 3. Code Optimization  Code Optimization Techniques  Constant propagation  Constant folding  Algebraic simplification, strength reduction  Copy propagation
  • 4. Code Optimization  Techniques for Speed optimization  Constant propagation  Constant folding  Algebraic simplification, strength reduction  Copy propagation  Common subexpression elimination  Unreacheable code elimination  Dead code elimination  Loop Optimization  Function related
  • 5. Code Optimization  Techniques for Memory optimization  Reduce Padding  Local Vs Global Variables
  • 6. Code Optimization Techniques  Constant Propagation  If the value of a variable is a constant, then replace the variable by the constant  E.g. N := 10; C := 2; for (i:=0; i<N; i++) { s := s + i*C; }  for (i:=0; i<10; i++) { s := s + i*2; }  Constant Folding  Simplifying constant expressions as much as possible  E.g. static final int a = 2; int b = 30 * a; // folding would create int b = 60;
  • 7. Code Optimization Techniques  Algebraic simplification  More general form of constant folding, e.g.,  x + 0  x x – 0  x  x * 1  x x / 1  x  x * 0  0  Repeatedly apply the rules  (y * 1 + 0) / 1  y  Strength reduction  Replace expensive operations  E.g., x := x * 8  x := x << 3
  • 8. Code Optimization Techniques  Code Motion (Out of loops)  A modification that decreases the amount of code in a loop.  Invariant expressions should be executed only once.  E.g. for (int i = 0; i < x.length; i++) x[i] *= Math.PI * Math.cos(y); Code motion will result in the equivalent of double picosy = Math.PI * Math.cos(y); for (int i = 0; i < x.length; i++) x[i] *= picosy;
  • 9. Code Optimization Techniques  Copy propagation  Extension of constant propagation  After y is assigned to x, use y to replace x till x is assigned again  Example x := y;  s := y * f(y) s := x * f(x)  Reduce the copying  If y is reassigned in between, then this action cannot be performed
  • 10. Code Optimization Techniques  Common sub expression elimination  Searches for instances of identical expression, replacing them with a single variable holding the computed value (if possible)  Example: a := b + c a := b + c c := b + c  c := a d := b + c d := b + c  Example in array index calculations  c[i+1] := a[i+1] + b[i+1]  During address computation, i+1 should be reused  Not visible in high level code, but in intermediate code
  • 11. Code Optimization Techniques  Unreacheable code elimination  Construct the control flow graph  Unreachable code block will not have an incoming edge  Dead code elimination  Ineffective statements  x := y + 1 (immediately redefined, eliminate!)  y := 5  y := 5  x := 2 * z x := 2 * z  A variable is dead if it is never used after last definition  Eliminate assignments to dead variables  Need to do data flow analysis to find dead variables
  • 12. Code Optimization Techniques  Function inlining  Replace a function call with the body of the function.  Program will spend less time in the function call and return parts, etc.  It has also some disadvantages – Increase program size and break encapsulation.  Function cloning  Create specialized code for a function for different calling parameters
  • 13. Code Optimization Techniques  Loop optimization  Is the process of the increasing execution speed and reducing the overheads associated of loops.  Consumes 90% of the execution time  a larger payoff to optimize the code within a loop  Techniques  Loop invariant detection and code motion  Induction variable elimination  Strength reduction in loops  Loop unrolling  Loop peeling  Loop fusion
  • 14. Code Optimization Techniques  Loop invariant detection and code motion  If the result of a statement or expression does not change within a loop, and it has no external side-effect  Computation can be moved to the outside of the loop  Example for (i=0; i<n; i++) a[i] := a[i] + x/y;  for (i=0; i<n; i++) { c := x/y; a[i] := a[i] + c; } // three address code  c := x/y; for (i=0; i<n; i++) a[i] := a[i] + c;  Induction variable elimination  If there are multiple induction variables in a loop, can eliminate the ones which are used only in the test condition  Example s := 0; for (i=0; i<n; i++) { s := s + 4; }  s := 0; while (s < 4*n) { s := s + 4; }
  • 15. Code Optimization Techniques  Strength reduction in loops  Example s := 0; for (i=0; i<n; i++) { v := 4 * i; s := s + v; )  s := 0; for (i=0; i<n; i++) { v := v + 4; s := s + v; )  Loop Termination  The loop termination condition can cause significant overhead if written without caution.  Should always write count-down-to-zero loops and use simple termination conditions.  Example int fact1_func (int n) { int i, fact = 1; for (i = 1; i <= n; i++)  int fact1_func (int n) { int i, fact = 1; for (i = 1; i <= n; i++)
  • 16. Code Optimization Techniques  Loop unrolling  Execute loop body multiple times at each iteration  Get rid of the conditional branches, if possible  Allow optimization to cross multiple iterations of the loop  Especially for parallel instruction execution  Space time tradeoff  Increase in code size, reduce some instruction  Example for(i=0; i<3; i++) { something(i); } After loop unrolling – for(i=0; i<3; i++){ something(0); something(1); something(2);}  Loop peeling  Similar to unrolling  But unroll the first and/or last iterations
  • 17. Code Optimization Techniques  Loop fusion  Some adjacent loops can be fused into one loop to reduce loop overhead and improve run-time performance.  Example for i=1 to N do A[i] = B[i] + 1 endfor for i=1 to N do C[i] = A[i] / 2 endfor for i=1 to N do D[i] = 1 / C[i+1] endforBefore Loop Fusion for i=1 to N do A[i] = B[i] + 1 C[i] = A[i] / 2 D[i] = 1 / C[i+1] endfor
  • 18. Code Optimization Techniques Memory based Optimization  Reduce Padding  Declare local variables based on their size. Prefer to declare larger to smaller data type variables.  E.g. struct { char x; int y; } Here size of struct is 4 bytes !!!!!  Points to be remember while using Register variable –
  • 19. Code Optimization Techniques  Let us discuss one more example – /* sizeof = 64 bytes */ /*sizeof = 48 bytes */ struct foo sturct foo { float a; { double b; double b; double d; float c; long f; double d; long h; short e; float c; long f; float a; short g; int j; long h; int l; char i; short e; int j; short g; char k; char k; int l; }; char I;};
  • 20. Code Optimization Techniques  Local Vs Global Variables  Local variables are stored in registers.  Keep the number of local variables low to allow them to be fit in the available registers and avoid stack usage.  Global Variables are stored in RAM.  Manipulating and accessing local variables is less costly as it uses registers compared to accessing from memory.  Avoid referring to global or static variables inside the tightest loops, instead use local variables.  Declare local variables in the inner most scope.
  • 21. Code Optimization Techniques  Locality of Reference  Local variables are stored in registers.  Keep the number of local variables low to allow them to be fit in the available registers and avoid stack usage.  Global Variables are stored in RAM.  Manipulating and accessing local variables is less costly as it uses registers compared to accessing from memory.  Avoid referring to global or static variables inside the tightest loops, instead use local variables.  Declare local variables in the inner most scope.
  • 22. Code Optimization tricks in „C‟ Language  Usages of Register Variables  Use Register variables whenever possible.  Accessed from register rather from memory, so accessing is faster.  E.g. register float val; register double dval; register int ival;  Points to be remember while using Register variable -  Only declare those variables as register which are heavily used  We can not take the address of register variable , so never try this. &ival would fail.
  • 23. Code Optimization tricks in „C‟ Language  String Operations  Most of the C library str* functions operate in time proportional to the length(s) of the string(s) they are given.  Avoid calling strlen( ) during a loop involving the string itself.  strcat( ) will scan the full (current) length of the string on each call. If you've been keeping track of the length anyway , you can index directly to the end of the string and strcpy to there.  Try to avoid stecmp( ) as much as possible. If is not then try to use it smartly!!!  Try to avoid empty string test using strlen() function. Avoid strlen(s) == 0 use *s == „0‟ Avoid strcpy(s, “ ”) use *s = „0‟
  • 24. Code Optimization tricks in „C‟ Language  Aliasing – It do optimization , try it!!!!  Consider the following: void func1( int *data ) { int i; for(i=0; i<10; i++) { somefunc2( *data, i); } }  Optimized code after aliasing : void func1( int *data ) { int i; int localdata; localdata = *data; for(i=0; i<10; i++) { somefunc2( localdata, i); } }
  • 25. Code Optimization tricks in „C‟ Language  Miscellaneous  Use unsigned int instead of int if we know the value will never be negative.  Avoid unnecessary division. For eg - (a / b) > c can be rewritten as a > (c * b)  Use shift operations >> and << instead of integer multiplication and division, where possible.  Try to use Switch in place of nested if…elseif…elseif…else  Prefer Integer comparison.  Avoid calculations in loop and try to use simplify expression.  Try to minimize usages of global variables. They are never allocated to registers.  use the prefix operator (++obj) instead of the postfix operator (obj++).
  • 26. Code Optimization tricks in „C‟ Language  Miscellaneous Continue…..  Use unsigned int instead of int if we know the value will never be negative.  Avoid unnecessary division.