SlideShare a Scribd company logo
1 of 15
Code Optimization
• Optimization is the process of transforming a piece of code to make more efficient 
(either in terms of time or space) without changing its output or side-effects. The only 
difference visible to the code’s user should be that it runs faster and/or consumes less 
memory. It is really a misnomer that the name implies you are finding an "optimal" 
solution— in truth, optimization aims to improve, not perfect, the result. Optimization is 
the field where most compiler research is done today. The tasks of the front-end 
(scanning, parsing, semantic analysis) are well understood and unoptimized code 
generation is relatively straightforward. 
• Optimization, on the other hand, still retains a sizable measure of mysticism. High-quality 
optimization is more of an art than a science. Compilers for mature languages aren’t 
judged by how well they parse or analyze the code—you just expect it to do it right with 
a minimum of hassle—but instead by the quality of the object code they produce. Many 
optimization problems are NP-complete and thus most optimization algorithms rely on 
heuristics and approximations. It may be possible to come up with a case where a 
particular algorithm fails to produce better code or perhaps even makes it worse. 
• However, the algorithms tend to do rather well overall. It’s worth reiterating here that 
efficient code starts with intelligent decisions by the programmer. No one expects a 
compiler to replace BubbleSort with Quicksort. If a programmer uses a lousy algorithm, 
no amount of optimization can make it snappy. In terms of big-O, a compiler can only 
make improvements to constant factors. But, all else being equal, you want an algorithm 
with low constant factors.
When and Where To Optimize 
• There are a variety of tactics for attacking optimization. Some techniques 
are applied to the intermediate code, to streamline, rearrange, compress, 
etc. in an effort to reduce the size of the abstract syntax tree or shrink the 
number of TAC instructions. Others are applied as part of final code 
generation—choosing which instructions to emit, how to allocate registers 
and when/what to spill, and the like. And still other optimizations may 
occur after final code generation, attempting to re-work the assembly code 
itself into something more efficient. 
• Optimization can be very complex and time-consuming; it often involves 
multiple subphases, some of which are applied more than once. Most 
compilers allow optimization to be turned off to speed up compilation (gcc 
even has specific flags to turn on and off individual optimizations.)
Constant Folding 
• Constant folding refers to the evaluation at compile-time of 
expressions whose 
• operands are known to be constant. In its simplest form, it involves 
determining that all of the operands in an expression are constant-valued, 
performing the evaluation of the expression at compile-time, 
and then replacing the expression by its value. If an expression such 
as 10 + 2 * 3 is encountered, the compiler can compute the result at 
compile-time (16) and emit code as if the input contained the result 
rather than the original expression. Similarly, constant conditions, 
such as a conditional branch if a < b goto L1 else goto L2 where a and 
b are constant can be replaced by a Goto L1 or Goto L2 depending on 
the truth of the expression evaluated at compile-time.
Copy Propagation 
• This optimization is similar to constant propagation, but generalized 
to non-constant values. If we have an assignment a = b in our 
instruction stream, we can replace later occurrences of a with b 
(assuming there are no changes to either variable in-between). Given 
the way we generate TAC code, this is a particularly valuable 
optimization sinceit is able to eliminate a large number of instructions 
that only serve to copy values from one variable to another.
Common Subexpression Elimination 
• Two operations are common if they produce the same result. In such 
a case, it is likely more efficient to compute the result once and 
reference it the second time rather than re-evaluate it. An expression 
is alive if the operands used to compute the expression have not been 
changed. An expression that is no longer alive is dead.
PROGRAM FOR CONSTANT FOLDING 
• #include<stdio.h> 
• #include<conio.h> 
• int fib(int base) 
• {int result,tmp0,tmp1,tmp2,temp[12],f0,f1,i; 
• temp[0]=1; 
• temp[4]=0; 
• temp[1]=base < temp[0]; 
• temp[2]=base==temp[0]; 
• temp[3]=temp[1]||temp[2];
• if(temp[3]==temp[4]) 
• goto L0; 
• result=base; 
• goto L1; 
• L0: 
• temp[4]=0; 
• f0=temp[4]; 
• tmp0=f0; 
• temp[5]=1; 
• f1=temp[5]; 
• tmp1=f1; 
• printf("%d %d ",f0,f1);
• temp[6]=2; 
• i=temp[6]; 
• tmp2=i; 
• L2: 
• temp[7]=i<base; 
• temp[8]=i==base; 
• temp[9]=temp[7]||temp[8]; 
• if(temp[4]==temp[9]) 
• goto L3;
• temp[10]=tmp0+tmp1; 
• result=temp[10]; 
• printf("%d ",result); 
• tmp0=tmp1; 
• tmp1=result; 
• temp[11]=1; 
• temp[12]=tmp2+temp[11]; 
• tmp2=temp[12]; 
• goto L2; 
• L3: 
• L1: 
• return result; 
• } 
• void main() 
• { 
• int n; 
• clrscr(); 
• printf("nEnter a number:"); 
• scanf("%d",&n) ; 
• printf("nnfibonacci is %d",fib(n)); 
• getch(); 
• }
PROGRAM FOR COPY PROPOGATION 
• #include<stdio.h> 
• #include<conio.h> 
• int fib(int base) 
• { 
• int result,tmp0,tmp1,tmp2; 
• if (base <= 1) 
• { 
• result = base; 
• } 
• else 
• {
• int i; 
• int f0; 
• int f1; 
• f0 = 0; 
• tmp0=f0; 
• f1 = 1; 
• tmp1=f1; 
• i = 2; 
• tmp2=i; 
• printf("nn%dt%d",tmp0,tmp1); 
• while (tmp2 <= base) 
• { 
• result = tmp0 + tmp1; 
• printf("t%d",result); 
• tmp0 = tmp1; 
• tmp1 = result; 
• tmp2 = tmp2 + 1; 
• } 
• } 
• return result; 
• }
• void main() 
• { 
• int n; 
• clrscr(); 
• printf("nnEnter number:"); 
• scanf("%d",&n); 
• printf("nnFibonacci is: %dn",fib(n)); 
• getch(); 
• }
PROGRAM FOR COMMON SUBEXPRESSION 
ELIMINATION 
• #include<stdio.h> 
• #include<conio.h> 
• void main() 
• { 
• int a,b,c,d,e,f; 
• clrscr(); 
• printf("n Enter the values of a,b,c"); 
• scanf("n %d %d %d",&a,&b,&c); 
• d=a+b; 
• e=d*b;
• f=e+d+a; 
• printf("n D=%d",d); 
• printf("n E=%d",e); 
• printf("n F=%d",f); 
• getch(); 
• }

More Related Content

What's hot

Optimization in Programming languages
Optimization in Programming languagesOptimization in Programming languages
Optimization in Programming languagesAnkit Pandey
 
Compiler optimization
Compiler optimizationCompiler optimization
Compiler optimizationKarthik Vivek
 
Peephole optimization techniques
Peephole optimization techniquesPeephole optimization techniques
Peephole optimization techniquesgarishma bhatia
 
Compiler optimization
Compiler optimizationCompiler optimization
Compiler optimizationZongYing Lyu
 
Compiler in System Programming/Code Optimization techniques in System Program...
Compiler in System Programming/Code Optimization techniques in System Program...Compiler in System Programming/Code Optimization techniques in System Program...
Compiler in System Programming/Code Optimization techniques in System Program...Janki Shah
 
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
 
02 intel v_tune_session_02
02 intel v_tune_session_0202 intel v_tune_session_02
02 intel v_tune_session_02Vivek chan
 
Control Flow Analysis
Control Flow AnalysisControl Flow Analysis
Control Flow AnalysisEdgar Barbosa
 
Compiler optimization
Compiler optimizationCompiler optimization
Compiler optimizationliu_ming50
 
Arm developement
Arm developementArm developement
Arm developementhirokiht
 
Compiler Optimization-Space Exploration
Compiler Optimization-Space ExplorationCompiler Optimization-Space Exploration
Compiler Optimization-Space Explorationtmusabbir
 
Finding Bugs Faster with Assertion Based Verification (ABV)
Finding Bugs Faster with Assertion Based Verification (ABV)Finding Bugs Faster with Assertion Based Verification (ABV)
Finding Bugs Faster with Assertion Based Verification (ABV)DVClub
 
Problem solving (C++ Programming)
Problem solving (C++ Programming)Problem solving (C++ Programming)
Problem solving (C++ Programming)Umair Younas
 
09 implementing+subprograms
09 implementing+subprograms09 implementing+subprograms
09 implementing+subprogramsbaran19901990
 

What's hot (20)

Optimization in Programming languages
Optimization in Programming languagesOptimization in Programming languages
Optimization in Programming languages
 
Compiler optimization
Compiler optimizationCompiler optimization
Compiler optimization
 
Unit 3 part2
Unit 3 part2Unit 3 part2
Unit 3 part2
 
Peephole optimization techniques
Peephole optimization techniquesPeephole optimization techniques
Peephole optimization techniques
 
Compiler optimization
Compiler optimizationCompiler optimization
Compiler optimization
 
Compiler in System Programming/Code Optimization techniques in System Program...
Compiler in System Programming/Code Optimization techniques in System Program...Compiler in System Programming/Code Optimization techniques in System Program...
Compiler in System Programming/Code Optimization techniques in System Program...
 
Plc part 3
Plc  part 3Plc  part 3
Plc part 3
 
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
 
02 intel v_tune_session_02
02 intel v_tune_session_0202 intel v_tune_session_02
02 intel v_tune_session_02
 
Peephole Optimization
Peephole OptimizationPeephole Optimization
Peephole Optimization
 
Control Flow Analysis
Control Flow AnalysisControl Flow Analysis
Control Flow Analysis
 
Compiler optimization
Compiler optimizationCompiler optimization
Compiler optimization
 
Arm developement
Arm developementArm developement
Arm developement
 
Compiler Optimization-Space Exploration
Compiler Optimization-Space ExplorationCompiler Optimization-Space Exploration
Compiler Optimization-Space Exploration
 
Finding Bugs Faster with Assertion Based Verification (ABV)
Finding Bugs Faster with Assertion Based Verification (ABV)Finding Bugs Faster with Assertion Based Verification (ABV)
Finding Bugs Faster with Assertion Based Verification (ABV)
 
Parallel concepts1
Parallel concepts1Parallel concepts1
Parallel concepts1
 
Problem solving (C++ Programming)
Problem solving (C++ Programming)Problem solving (C++ Programming)
Problem solving (C++ Programming)
 
C- language Lecture 4
C- language Lecture 4C- language Lecture 4
C- language Lecture 4
 
Recursion
RecursionRecursion
Recursion
 
09 implementing+subprograms
09 implementing+subprograms09 implementing+subprograms
09 implementing+subprograms
 

Viewers also liked (12)

States machine
States machineStates machine
States machine
 
Keyword Presentation
Keyword PresentationKeyword Presentation
Keyword Presentation
 
Types and roles
Types and rolesTypes and roles
Types and roles
 
Uml Common Mechanism
Uml Common MechanismUml Common Mechanism
Uml Common Mechanism
 
Processes and threads
Processes and threadsProcesses and threads
Processes and threads
 
Compilers
CompilersCompilers
Compilers
 
What is symbol table?
What is symbol table?What is symbol table?
What is symbol table?
 
Single Pass Assembler
Single Pass AssemblerSingle Pass Assembler
Single Pass Assembler
 
Linux programming - Getting self started
Linux programming - Getting self started Linux programming - Getting self started
Linux programming - Getting self started
 
Linux Internals - Interview essentials 4.0
Linux Internals - Interview essentials 4.0Linux Internals - Interview essentials 4.0
Linux Internals - Interview essentials 4.0
 
Operating Systems: Processor Management
Operating Systems: Processor ManagementOperating Systems: Processor Management
Operating Systems: Processor Management
 
Two pass Assembler
Two pass AssemblerTwo pass Assembler
Two pass Assembler
 

Similar to sCode optimization

Introduction to code optimization by dipankar
Introduction to code optimization by dipankarIntroduction to code optimization by dipankar
Introduction to code optimization by dipankarDipankar Nalui
 
Algorithm Analysis.pdf
Algorithm Analysis.pdfAlgorithm Analysis.pdf
Algorithm Analysis.pdfNayanChandak1
 
CODE TUNINGtertertertrtryryryryrtytrytrtry
CODE TUNINGtertertertrtryryryryrtytrytrtryCODE TUNINGtertertertrtryryryryrtytrytrtry
CODE TUNINGtertertertrtryryryryrtytrytrtrykapib57390
 
Software development best practices & coding guidelines
Software development best practices & coding guidelinesSoftware development best practices & coding guidelines
Software development best practices & coding guidelinesAnkur Goyal
 
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
 
Cs 568 Spring 10 Lecture 5 Estimation
Cs 568 Spring 10  Lecture 5 EstimationCs 568 Spring 10  Lecture 5 Estimation
Cs 568 Spring 10 Lecture 5 EstimationLawrence Bernstein
 
Design Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesDesign Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesInductive Automation
 
Design Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesDesign Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesInductive Automation
 
FPL -Part 2 ( Sem - I 2013)
FPL -Part 2 ( Sem - I 2013)FPL -Part 2 ( Sem - I 2013)
FPL -Part 2 ( Sem - I 2013)Yogesh Deshpande
 
Algorithm and C code related to data structure
Algorithm and C code related to data structureAlgorithm and C code related to data structure
Algorithm and C code related to data structureSelf-Employed
 
2. Introduction to Algorithm.pptx
2. Introduction to Algorithm.pptx2. Introduction to Algorithm.pptx
2. Introduction to Algorithm.pptxRahikAhmed1
 
Effective cplusplus
Effective cplusplusEffective cplusplus
Effective cplusplusMark Veltzer
 
What’s eating python performance
What’s eating python performanceWhat’s eating python performance
What’s eating python performancePiotr Przymus
 

Similar to sCode optimization (20)

Introduction to code optimization by dipankar
Introduction to code optimization by dipankarIntroduction to code optimization by dipankar
Introduction to code optimization by dipankar
 
Algorithm Analysis.pdf
Algorithm Analysis.pdfAlgorithm Analysis.pdf
Algorithm Analysis.pdf
 
CODE TUNINGtertertertrtryryryryrtytrytrtry
CODE TUNINGtertertertrtryryryryrtytrytrtryCODE TUNINGtertertertrtryryryryrtytrytrtry
CODE TUNINGtertertertrtryryryryrtytrytrtry
 
Software development best practices & coding guidelines
Software development best practices & coding guidelinesSoftware development best practices & coding guidelines
Software development best practices & coding guidelines
 
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
 
Cs 568 Spring 10 Lecture 5 Estimation
Cs 568 Spring 10  Lecture 5 EstimationCs 568 Spring 10  Lecture 5 Estimation
Cs 568 Spring 10 Lecture 5 Estimation
 
Survelaine murillo ppt
Survelaine murillo pptSurvelaine murillo ppt
Survelaine murillo ppt
 
Design Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesDesign Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best Practices
 
Design Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesDesign Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best Practices
 
Logic programming in python
Logic programming in pythonLogic programming in python
Logic programming in python
 
FPL -Part 2 ( Sem - I 2013)
FPL -Part 2 ( Sem - I 2013)FPL -Part 2 ( Sem - I 2013)
FPL -Part 2 ( Sem - I 2013)
 
Algorithm and C code related to data structure
Algorithm and C code related to data structureAlgorithm and C code related to data structure
Algorithm and C code related to data structure
 
2. Introduction to Algorithm.pptx
2. Introduction to Algorithm.pptx2. Introduction to Algorithm.pptx
2. Introduction to Algorithm.pptx
 
Code Tuning
Code TuningCode Tuning
Code Tuning
 
Introduction to C ++.pptx
Introduction to C ++.pptxIntroduction to C ++.pptx
Introduction to C ++.pptx
 
Effective cplusplus
Effective cplusplusEffective cplusplus
Effective cplusplus
 
pm1
pm1pm1
pm1
 
Python algorithm
Python algorithmPython algorithm
Python algorithm
 
DAA Unit 1.pdf
DAA Unit 1.pdfDAA Unit 1.pdf
DAA Unit 1.pdf
 
What’s eating python performance
What’s eating python performanceWhat’s eating python performance
What’s eating python performance
 

More from Satyamevjayte Haxor (9)

Patterns
PatternsPatterns
Patterns
 
Uml class Diagram
Uml class DiagramUml class Diagram
Uml class Diagram
 
Lexical
LexicalLexical
Lexical
 
Linker
LinkerLinker
Linker
 
Nested micro
Nested microNested micro
Nested micro
 
Multiplier control unit
Multiplier control unitMultiplier control unit
Multiplier control unit
 
Control unit design
Control unit designControl unit design
Control unit design
 
Direct linking loaders
Direct linking loadersDirect linking loaders
Direct linking loaders
 
Linking in MS-Dos System
Linking in MS-Dos SystemLinking in MS-Dos System
Linking in MS-Dos System
 

Recently uploaded

WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...
WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...
WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...WSO2
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2
 
WSO2CON 2024 - Building a Digital Government in Uganda
WSO2CON 2024 - Building a Digital Government in UgandaWSO2CON 2024 - Building a Digital Government in Uganda
WSO2CON 2024 - Building a Digital Government in UgandaWSO2
 
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...WSO2
 
WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...
WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...
WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...WSO2
 
WSO2CON 2024 - Architecting AI in the Enterprise: APIs and Applications
WSO2CON 2024 - Architecting AI in the Enterprise: APIs and ApplicationsWSO2CON 2024 - Architecting AI in the Enterprise: APIs and Applications
WSO2CON 2024 - Architecting AI in the Enterprise: APIs and ApplicationsWSO2
 
Evolving Data Governance for the Real-time Streaming and AI Era
Evolving Data Governance for the Real-time Streaming and AI EraEvolving Data Governance for the Real-time Streaming and AI Era
Evolving Data Governance for the Real-time Streaming and AI Eraconfluent
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2
 
WSO2CON 2024 Slides - Unlocking Value with AI
WSO2CON 2024 Slides - Unlocking Value with AIWSO2CON 2024 Slides - Unlocking Value with AI
WSO2CON 2024 Slides - Unlocking Value with AIWSO2
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastPapp Krisztián
 
Driving Innovation: Scania's API Revolution with WSO2
Driving Innovation: Scania's API Revolution with WSO2Driving Innovation: Scania's API Revolution with WSO2
Driving Innovation: Scania's API Revolution with WSO2WSO2
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in sowetomasabamasaba
 
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public AdministrationWSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public AdministrationWSO2
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2
 
WSO2CON 2024 - Designing Event-Driven Enterprises: Stories of Transformation
WSO2CON 2024 - Designing Event-Driven Enterprises: Stories of TransformationWSO2CON 2024 - Designing Event-Driven Enterprises: Stories of Transformation
WSO2CON 2024 - Designing Event-Driven Enterprises: Stories of TransformationWSO2
 
WSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - KanchanaWSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - KanchanaWSO2
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...
WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...
WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...WSO2
 

Recently uploaded (20)

WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...
WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...
WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
WSO2CON 2024 - Building a Digital Government in Uganda
WSO2CON 2024 - Building a Digital Government in UgandaWSO2CON 2024 - Building a Digital Government in Uganda
WSO2CON 2024 - Building a Digital Government in Uganda
 
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...
 
WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...
WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...
WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...
 
WSO2CON 2024 - Architecting AI in the Enterprise: APIs and Applications
WSO2CON 2024 - Architecting AI in the Enterprise: APIs and ApplicationsWSO2CON 2024 - Architecting AI in the Enterprise: APIs and Applications
WSO2CON 2024 - Architecting AI in the Enterprise: APIs and Applications
 
Evolving Data Governance for the Real-time Streaming and AI Era
Evolving Data Governance for the Real-time Streaming and AI EraEvolving Data Governance for the Real-time Streaming and AI Era
Evolving Data Governance for the Real-time Streaming and AI Era
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
WSO2CON 2024 Slides - Unlocking Value with AI
WSO2CON 2024 Slides - Unlocking Value with AIWSO2CON 2024 Slides - Unlocking Value with AI
WSO2CON 2024 Slides - Unlocking Value with AI
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - Keynote
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
Driving Innovation: Scania's API Revolution with WSO2
Driving Innovation: Scania's API Revolution with WSO2Driving Innovation: Scania's API Revolution with WSO2
Driving Innovation: Scania's API Revolution with WSO2
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public AdministrationWSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
 
WSO2CON 2024 - Designing Event-Driven Enterprises: Stories of Transformation
WSO2CON 2024 - Designing Event-Driven Enterprises: Stories of TransformationWSO2CON 2024 - Designing Event-Driven Enterprises: Stories of Transformation
WSO2CON 2024 - Designing Event-Driven Enterprises: Stories of Transformation
 
WSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - KanchanaWSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - Kanchana
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...
WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...
WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...
 

sCode optimization

  • 2. • Optimization is the process of transforming a piece of code to make more efficient (either in terms of time or space) without changing its output or side-effects. The only difference visible to the code’s user should be that it runs faster and/or consumes less memory. It is really a misnomer that the name implies you are finding an "optimal" solution— in truth, optimization aims to improve, not perfect, the result. Optimization is the field where most compiler research is done today. The tasks of the front-end (scanning, parsing, semantic analysis) are well understood and unoptimized code generation is relatively straightforward. • Optimization, on the other hand, still retains a sizable measure of mysticism. High-quality optimization is more of an art than a science. Compilers for mature languages aren’t judged by how well they parse or analyze the code—you just expect it to do it right with a minimum of hassle—but instead by the quality of the object code they produce. Many optimization problems are NP-complete and thus most optimization algorithms rely on heuristics and approximations. It may be possible to come up with a case where a particular algorithm fails to produce better code or perhaps even makes it worse. • However, the algorithms tend to do rather well overall. It’s worth reiterating here that efficient code starts with intelligent decisions by the programmer. No one expects a compiler to replace BubbleSort with Quicksort. If a programmer uses a lousy algorithm, no amount of optimization can make it snappy. In terms of big-O, a compiler can only make improvements to constant factors. But, all else being equal, you want an algorithm with low constant factors.
  • 3. When and Where To Optimize • There are a variety of tactics for attacking optimization. Some techniques are applied to the intermediate code, to streamline, rearrange, compress, etc. in an effort to reduce the size of the abstract syntax tree or shrink the number of TAC instructions. Others are applied as part of final code generation—choosing which instructions to emit, how to allocate registers and when/what to spill, and the like. And still other optimizations may occur after final code generation, attempting to re-work the assembly code itself into something more efficient. • Optimization can be very complex and time-consuming; it often involves multiple subphases, some of which are applied more than once. Most compilers allow optimization to be turned off to speed up compilation (gcc even has specific flags to turn on and off individual optimizations.)
  • 4. Constant Folding • Constant folding refers to the evaluation at compile-time of expressions whose • operands are known to be constant. In its simplest form, it involves determining that all of the operands in an expression are constant-valued, performing the evaluation of the expression at compile-time, and then replacing the expression by its value. If an expression such as 10 + 2 * 3 is encountered, the compiler can compute the result at compile-time (16) and emit code as if the input contained the result rather than the original expression. Similarly, constant conditions, such as a conditional branch if a < b goto L1 else goto L2 where a and b are constant can be replaced by a Goto L1 or Goto L2 depending on the truth of the expression evaluated at compile-time.
  • 5. Copy Propagation • This optimization is similar to constant propagation, but generalized to non-constant values. If we have an assignment a = b in our instruction stream, we can replace later occurrences of a with b (assuming there are no changes to either variable in-between). Given the way we generate TAC code, this is a particularly valuable optimization sinceit is able to eliminate a large number of instructions that only serve to copy values from one variable to another.
  • 6. Common Subexpression Elimination • Two operations are common if they produce the same result. In such a case, it is likely more efficient to compute the result once and reference it the second time rather than re-evaluate it. An expression is alive if the operands used to compute the expression have not been changed. An expression that is no longer alive is dead.
  • 7. PROGRAM FOR CONSTANT FOLDING • #include<stdio.h> • #include<conio.h> • int fib(int base) • {int result,tmp0,tmp1,tmp2,temp[12],f0,f1,i; • temp[0]=1; • temp[4]=0; • temp[1]=base < temp[0]; • temp[2]=base==temp[0]; • temp[3]=temp[1]||temp[2];
  • 8. • if(temp[3]==temp[4]) • goto L0; • result=base; • goto L1; • L0: • temp[4]=0; • f0=temp[4]; • tmp0=f0; • temp[5]=1; • f1=temp[5]; • tmp1=f1; • printf("%d %d ",f0,f1);
  • 9. • temp[6]=2; • i=temp[6]; • tmp2=i; • L2: • temp[7]=i<base; • temp[8]=i==base; • temp[9]=temp[7]||temp[8]; • if(temp[4]==temp[9]) • goto L3;
  • 10. • temp[10]=tmp0+tmp1; • result=temp[10]; • printf("%d ",result); • tmp0=tmp1; • tmp1=result; • temp[11]=1; • temp[12]=tmp2+temp[11]; • tmp2=temp[12]; • goto L2; • L3: • L1: • return result; • } • void main() • { • int n; • clrscr(); • printf("nEnter a number:"); • scanf("%d",&n) ; • printf("nnfibonacci is %d",fib(n)); • getch(); • }
  • 11. PROGRAM FOR COPY PROPOGATION • #include<stdio.h> • #include<conio.h> • int fib(int base) • { • int result,tmp0,tmp1,tmp2; • if (base <= 1) • { • result = base; • } • else • {
  • 12. • int i; • int f0; • int f1; • f0 = 0; • tmp0=f0; • f1 = 1; • tmp1=f1; • i = 2; • tmp2=i; • printf("nn%dt%d",tmp0,tmp1); • while (tmp2 <= base) • { • result = tmp0 + tmp1; • printf("t%d",result); • tmp0 = tmp1; • tmp1 = result; • tmp2 = tmp2 + 1; • } • } • return result; • }
  • 13. • void main() • { • int n; • clrscr(); • printf("nnEnter number:"); • scanf("%d",&n); • printf("nnFibonacci is: %dn",fib(n)); • getch(); • }
  • 14. PROGRAM FOR COMMON SUBEXPRESSION ELIMINATION • #include<stdio.h> • #include<conio.h> • void main() • { • int a,b,c,d,e,f; • clrscr(); • printf("n Enter the values of a,b,c"); • scanf("n %d %d %d",&a,&b,&c); • d=a+b; • e=d*b;
  • 15. • f=e+d+a; • printf("n D=%d",d); • printf("n E=%d",e); • printf("n F=%d",f); • getch(); • }