SlideShare a Scribd company logo
C Optimization
Techniques
Team Emertxe
Optimization ?
✔ Program optimization or software optimization is
the process of modifying a software system to make
some aspect of it work more efficiently or use fewer
resources.
✔ Optimization is a process of improving efficiency of
a program in time (speed) or Space (size).
Categories of
Optimization ?
✔ Space Optimization Time Optimization
General
Phenomenon
Note : However this may not always true.
Code size speed
Bigger Greater
Smaller Lesser
Compiler
Code Optimization
✔ Compilers can be designed to provide code
optimization.
✔ Users should only focus on optimization not
provided by the compiler such as choosing a faster
and/or less memory intensive algorithm
Optimization:
Methods
✔ Choice of Compiler
✔ Compiler settings
✔ Programming Algorithm and Techniques
✔ Rewriting program in Assembly
✔ Code profilers
✔ Disassemble & code analysis
Programming
Algorithms & Techniques
✔ Data Handling
✔ Flow control
✔ Other Handling
Data Handling
✔ RAM Usage
✔ Data Types Usage
✔ Avoid Type Coversion
✔ Unsigned Advantage
✔ Float & Double
✔ Constant & Volatile
✔ Data Alignment – Arrangement & Packing
✔ Pass by Reference
RAM Usage
✔ Four main Component of program memory
Text
Data
Heap
Stack
✔ The reduction in one component will enable the increase in the
others.
✔ Unlike stack and heap, which are dynamic in nature, global data is
fixed in size.
Data Usage Types
The use of correct data type is important in a recursive calculation or
large array processing.
The extra size, which is not required, is taking up much space and
processing time.
Example:
If your variable range is varying in between 0 to 200 then you should take variable type
of unsigned char.
Avoid
Type Conversion
✔ Programmers should plan to use the same type of variables for
processing. Type conversion must be avoided.
✔ Otherwise, precious cycles will be waste to convert one type to
another (here Signed and Unsigned variables are considered as
different types.)
Usage: Signed &
Unsigned
In summary,
✔ use unsigned types for:
Division and remainders
Loop counters
Array indexing
✔ Use signed types for:
Integer-to-floating-point conversion
Floats & Doubles
✔ Maximum value of Float = 0x7F7F FFFF
✔ Maximum Value of Double = 0x7F7F FFFF FFFF FFFF
✔ To avoid the unnecessary type conversion or confusion,
programmers can assign the letter 'f' following the numeric value.
x = y + 0.2f;
Const & Volatile
✔ The “const” keyword is to define the data as a constant, which will
allocate it in the ROM space.
✔ Otherwise a RAM space will also be reserved for this data. This is
unnecessary as the constant data is supposed to be read-only.
Data Alignment :
Arrangment and Packing
✔ The declaration of the component in a structure will determine how
the components are being stored.
✔ Due to the Memory alignment, it is possible to have dummy area
within the structure. It is advised to place all similar size variables
in the same group.
Data Arrangment
Structure Padding
✔ As the structure is packed, integer b will not be aligned. This will
improve the RAM size but operational speed will be degraded, as the
access of ‘b’ will take up two cycles.
Pass by Reference
✔ Larger numbers of parameters may be costly due to the number of
pushing and popping actions on each function call.
✔ It is more efficient to pass structure reference as parameters to
reduce this overhead.
Arrays & Structure:
Initialization
✔ Illustration
Return Value
✔ The return value of a function will be stored in a register. If this
return data has no intended usage, time and space are wasted in
storing this information.
✔ Programmer should define the function as “void” to minimize the
extra handling in the function.
Flow Control
✔ Use of switch statement
✔ Inline function
✔ Loop unrolling
✔ Loop Hoisting
✔ Loop Overhead
Switch & Non-Contiguous
Case Expressions
✔ Use if-else statements in place of switch statements that have
noncontiguous case expressions.
✔ If the case expressions are contiguous or nearly contiguous integer
values, most compilers translate the switch statement as a jump table
instead of a comparison chain.
Inline Function
✔ The technique will cause the compiler to replace all calls to the
function, with a copy of the function’s code.
✔ This will eliminate the runtime overhead associated with the function
call.
✔ This is most effective if the function is called frequently, but contains
only a few lines of code.
✔ In-lining large functions will make the executable too large.
Loop Unrolling
✔ Loop unrolling tries to get rid of the checks completely or to reduce
the number of checks.
✔ If you know a loop is only performed a certain number of times, or if
you know the number of times it will be repeated is a multiple of a
constant you can unroll this loop.
Loop Hoisting
✔ Moving computations outside loops
✔ Saves computing time
Loop Overhead
✔ The MCU have a conditional branch mechanism that works well when
counting down from positive number to zero.
✔ It is easier to detect “Zero” (Branch non zero – single instruction) than
a “predefined number” (Compare and perform jump – two
instructions)
Other Handling
✔ Use of operators
✔ Use formula
✔ Inline Assembly
✔ Fixed point & Floating point
Use of operators
Replacing: Integer
Division With Multiplication
✔ Replace integer division with multiplication when there are multiple
divisions in an expression.
✔ This is possible only if no overflow will occur during the
computation of the product. The possibility of an overflow can be
determined by considering the possible ranges of the divisors.
✔ Integer division is the slowest of all integer arithmetic operations.
Example
Use finite differences to
Avoid multiplies
Module by &
Extracting common
subexpressions
✔ Manually extract common subexpressions where C compilers may be
unable to extract them from floating-point expressions due to the
guarantee against reordering of such expressions in the ANSI
standard.
Manual Optimization
Example 1
Manual Optimization
Example 2
Use Formula
✔ Use formula instead of loops
✔ What is the formula for sum of naturals numbers?
Example
Use Formula
Example
Example
Inline Assembly
In case you want write code is best optimized assembly code,
__asm__ ("movl %eax, %ebxnt"
"movl $56, %esint"
"movl %ecx, $label(%edx,%ebx,$4)nt"
"movb %ah, (%ebx)");
THANK YOU

More Related Content

What's hot (20)

PDF
ARM Architecture
Dwight Sabio
 
DOCX
Hardware-Software Codesign
Sudhanshu Janwadkar
 
PDF
RTOS for Embedded System Design
anand hd
 
PPTX
ARM Processors
Mathivanan Natarajan
 
PPTX
Interfacing memory with 8086 microprocessor
Vikas Gupta
 
PDF
Typical Embedded System
anand hd
 
PPTX
Pic microcontroller architecture
DominicHendry
 
PPTX
Vhdl programming
Yogesh Mashalkar
 
PDF
Multiple Access
RAVIKIRAN ANANDE
 
PPT
Arm processor
SHREEHARI WADAWADAGI
 
PPT
Communication Interface of The Embedded Systems
VijayKumar5738
 
PDF
ARM CORTEX M3 PPT
Gaurav Verma
 
PDF
Communication Protocols (UART, SPI,I2C)
Emertxe Information Technologies Pvt Ltd
 
PPT
Interfacing LCD with 8051 Microcontroller
Pantech ProLabs India Pvt Ltd
 
PPTX
Fpga architectures and applications
Sudhanshu Janwadkar
 
PPTX
Hardware Software Codesign
destruck
 
PPTX
Handoff in Mobile Communication
Noushad Hasan
 
PPT
Embedded system design process
Rayees CK
 
PDF
Jtag presentation
klinetik
 
ARM Architecture
Dwight Sabio
 
Hardware-Software Codesign
Sudhanshu Janwadkar
 
RTOS for Embedded System Design
anand hd
 
ARM Processors
Mathivanan Natarajan
 
Interfacing memory with 8086 microprocessor
Vikas Gupta
 
Typical Embedded System
anand hd
 
Pic microcontroller architecture
DominicHendry
 
Vhdl programming
Yogesh Mashalkar
 
Multiple Access
RAVIKIRAN ANANDE
 
Arm processor
SHREEHARI WADAWADAGI
 
Communication Interface of The Embedded Systems
VijayKumar5738
 
ARM CORTEX M3 PPT
Gaurav Verma
 
Communication Protocols (UART, SPI,I2C)
Emertxe Information Technologies Pvt Ltd
 
Interfacing LCD with 8051 Microcontroller
Pantech ProLabs India Pvt Ltd
 
Fpga architectures and applications
Sudhanshu Janwadkar
 
Hardware Software Codesign
destruck
 
Handoff in Mobile Communication
Noushad Hasan
 
Embedded system design process
Rayees CK
 
Jtag presentation
klinetik
 

Viewers also liked (17)

PPTX
Compiler in System Programming/Code Optimization techniques in System Program...
Janki Shah
 
PDF
Multiple Inheritance
adil raja
 
PDF
I2C programming with C and Arduino
sato262
 
PPT
Protols used in bluetooth
Sonali Parab
 
PPTX
Inheritance in OOPS
Ronak Chhajed
 
PPTX
Serial Peripheral Interface
Anurag Tomar
 
ODP
Arm developement
hirokiht
 
PPTX
I2C Protocol
Abhijeet kapse
 
PDF
SPI Protocol
Anurag Tomar
 
PDF
Object-Oriented Design: Multiple inheritance (C++ and C#)
Adair Dingle
 
PDF
Introduction to Embedded System
Emertxe Information Technologies Pvt Ltd
 
PDF
I2C Bus (Inter-Integrated Circuit)
Varun Mahajan
 
PPTX
Serial peripheral interface
Abhijeet kapse
 
PPT
Serial Peripheral Interface(SPI)
Dhaval Kaneria
 
PPT
Code Optimization
guest9f8315
 
PPTX
Iot
Ankit Anand
 
Compiler in System Programming/Code Optimization techniques in System Program...
Janki Shah
 
Multiple Inheritance
adil raja
 
I2C programming with C and Arduino
sato262
 
Protols used in bluetooth
Sonali Parab
 
Inheritance in OOPS
Ronak Chhajed
 
Serial Peripheral Interface
Anurag Tomar
 
Arm developement
hirokiht
 
I2C Protocol
Abhijeet kapse
 
SPI Protocol
Anurag Tomar
 
Object-Oriented Design: Multiple inheritance (C++ and C#)
Adair Dingle
 
Introduction to Embedded System
Emertxe Information Technologies Pvt Ltd
 
I2C Bus (Inter-Integrated Circuit)
Varun Mahajan
 
Serial peripheral interface
Abhijeet kapse
 
Serial Peripheral Interface(SPI)
Dhaval Kaneria
 
Code Optimization
guest9f8315
 
Ad

Similar to Embedded C - Optimization techniques (20)

PPTX
Mod.2.pptx
Kokilak27
 
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
 
PDF
Optimization in Programming languages
Ankit Pandey
 
PPTX
sCode optimization
Satyamevjayte Haxor
 
PPTX
Compiler optimization techniques
Hardik Devani
 
PPT
C tutorial
Anurag Sukhija
 
PDF
OptimizingARM
Jan-Lieuwe Koopmans
 
PDF
PPU Optimisation Lesson
slantsixgames
 
PPT
Code Tuning
guest4df97e3d
 
PPT
Code Optimization Lec#7.ppt Code Optimizer
zeeshanmubeen1
 
PDF
C optimization notes
Fyaz Ghaffar
 
Mod.2.pptx
Kokilak27
 
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
 
Optimization in Programming languages
Ankit Pandey
 
sCode optimization
Satyamevjayte Haxor
 
Compiler optimization techniques
Hardik Devani
 
C tutorial
Anurag Sukhija
 
OptimizingARM
Jan-Lieuwe Koopmans
 
PPU Optimisation Lesson
slantsixgames
 
Code Tuning
guest4df97e3d
 
Code Optimization Lec#7.ppt Code Optimizer
zeeshanmubeen1
 
C optimization notes
Fyaz Ghaffar
 
Ad

More from Emertxe Information Technologies Pvt Ltd (20)

Recently uploaded (20)

PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PDF
What Makes Contify’s News API Stand Out: Key Features at a Glance
Contify
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
DOCX
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PDF
Biography of Daniel Podor.pdf
Daniel Podor
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PDF
Advancing WebDriver BiDi support in WebKit
Igalia
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
What Makes Contify’s News API Stand Out: Key Features at a Glance
Contify
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
Biography of Daniel Podor.pdf
Daniel Podor
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
Advancing WebDriver BiDi support in WebKit
Igalia
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 

Embedded C - Optimization techniques

  • 2. Optimization ? ✔ Program optimization or software optimization is the process of modifying a software system to make some aspect of it work more efficiently or use fewer resources. ✔ Optimization is a process of improving efficiency of a program in time (speed) or Space (size).
  • 3. Categories of Optimization ? ✔ Space Optimization Time Optimization
  • 4. General Phenomenon Note : However this may not always true. Code size speed Bigger Greater Smaller Lesser
  • 5. Compiler Code Optimization ✔ Compilers can be designed to provide code optimization. ✔ Users should only focus on optimization not provided by the compiler such as choosing a faster and/or less memory intensive algorithm
  • 6. Optimization: Methods ✔ Choice of Compiler ✔ Compiler settings ✔ Programming Algorithm and Techniques ✔ Rewriting program in Assembly ✔ Code profilers ✔ Disassemble & code analysis
  • 7. Programming Algorithms & Techniques ✔ Data Handling ✔ Flow control ✔ Other Handling
  • 8. Data Handling ✔ RAM Usage ✔ Data Types Usage ✔ Avoid Type Coversion ✔ Unsigned Advantage ✔ Float & Double ✔ Constant & Volatile ✔ Data Alignment – Arrangement & Packing ✔ Pass by Reference
  • 9. RAM Usage ✔ Four main Component of program memory Text Data Heap Stack ✔ The reduction in one component will enable the increase in the others. ✔ Unlike stack and heap, which are dynamic in nature, global data is fixed in size.
  • 10. Data Usage Types The use of correct data type is important in a recursive calculation or large array processing. The extra size, which is not required, is taking up much space and processing time. Example: If your variable range is varying in between 0 to 200 then you should take variable type of unsigned char.
  • 11. Avoid Type Conversion ✔ Programmers should plan to use the same type of variables for processing. Type conversion must be avoided. ✔ Otherwise, precious cycles will be waste to convert one type to another (here Signed and Unsigned variables are considered as different types.)
  • 12. Usage: Signed & Unsigned In summary, ✔ use unsigned types for: Division and remainders Loop counters Array indexing ✔ Use signed types for: Integer-to-floating-point conversion
  • 13. Floats & Doubles ✔ Maximum value of Float = 0x7F7F FFFF ✔ Maximum Value of Double = 0x7F7F FFFF FFFF FFFF ✔ To avoid the unnecessary type conversion or confusion, programmers can assign the letter 'f' following the numeric value. x = y + 0.2f;
  • 14. Const & Volatile ✔ The “const” keyword is to define the data as a constant, which will allocate it in the ROM space. ✔ Otherwise a RAM space will also be reserved for this data. This is unnecessary as the constant data is supposed to be read-only.
  • 15. Data Alignment : Arrangment and Packing ✔ The declaration of the component in a structure will determine how the components are being stored. ✔ Due to the Memory alignment, it is possible to have dummy area within the structure. It is advised to place all similar size variables in the same group.
  • 17. Structure Padding ✔ As the structure is packed, integer b will not be aligned. This will improve the RAM size but operational speed will be degraded, as the access of ‘b’ will take up two cycles.
  • 18. Pass by Reference ✔ Larger numbers of parameters may be costly due to the number of pushing and popping actions on each function call. ✔ It is more efficient to pass structure reference as parameters to reduce this overhead.
  • 20. Return Value ✔ The return value of a function will be stored in a register. If this return data has no intended usage, time and space are wasted in storing this information. ✔ Programmer should define the function as “void” to minimize the extra handling in the function.
  • 21. Flow Control ✔ Use of switch statement ✔ Inline function ✔ Loop unrolling ✔ Loop Hoisting ✔ Loop Overhead
  • 22. Switch & Non-Contiguous Case Expressions ✔ Use if-else statements in place of switch statements that have noncontiguous case expressions. ✔ If the case expressions are contiguous or nearly contiguous integer values, most compilers translate the switch statement as a jump table instead of a comparison chain.
  • 23. Inline Function ✔ The technique will cause the compiler to replace all calls to the function, with a copy of the function’s code. ✔ This will eliminate the runtime overhead associated with the function call. ✔ This is most effective if the function is called frequently, but contains only a few lines of code. ✔ In-lining large functions will make the executable too large.
  • 24. Loop Unrolling ✔ Loop unrolling tries to get rid of the checks completely or to reduce the number of checks. ✔ If you know a loop is only performed a certain number of times, or if you know the number of times it will be repeated is a multiple of a constant you can unroll this loop.
  • 25. Loop Hoisting ✔ Moving computations outside loops ✔ Saves computing time
  • 26. Loop Overhead ✔ The MCU have a conditional branch mechanism that works well when counting down from positive number to zero. ✔ It is easier to detect “Zero” (Branch non zero – single instruction) than a “predefined number” (Compare and perform jump – two instructions)
  • 27. Other Handling ✔ Use of operators ✔ Use formula ✔ Inline Assembly ✔ Fixed point & Floating point
  • 29. Replacing: Integer Division With Multiplication ✔ Replace integer division with multiplication when there are multiple divisions in an expression. ✔ This is possible only if no overflow will occur during the computation of the product. The possibility of an overflow can be determined by considering the possible ranges of the divisors. ✔ Integer division is the slowest of all integer arithmetic operations. Example
  • 30. Use finite differences to Avoid multiplies Module by &
  • 31. Extracting common subexpressions ✔ Manually extract common subexpressions where C compilers may be unable to extract them from floating-point expressions due to the guarantee against reordering of such expressions in the ANSI standard.
  • 34. Use Formula ✔ Use formula instead of loops ✔ What is the formula for sum of naturals numbers? Example
  • 36. Inline Assembly In case you want write code is best optimized assembly code, __asm__ ("movl %eax, %ebxnt" "movl $56, %esint" "movl %ecx, $label(%edx,%ebx,$4)nt" "movb %ah, (%ebx)");