30 Proven C Optimization Tricks to Supercharge Embedded Code Performance
This article presents a comprehensive collection of low‑level C optimization techniques for embedded systems, covering algorithm selection, data type choices, arithmetic shortcuts, loop transformations, structure layout, function inlining, register usage, and many practical code examples to dramatically improve execution speed and reduce code size.
1. Choose Appropriate Algorithms and Data Structures
Select data structures that match the operation patterns; for frequent insertions and deletions, linked lists are faster than arrays. Prefer pointer arithmetic over array indexing for tighter, faster code, especially with multi‑dimensional arrays.
/* Example: pointer vs array indexing */
for (;;) {
A = array[t++];
a = *(p++);
}2. Use the Smallest Viable Data Types
Prefer char over int, int over long, and avoid float unless necessary, because smaller types generate shorter, faster machine code.
3. Reduce Arithmetic Intensity
Replace expensive operations with cheaper equivalents: use bitwise & instead of modulus for powers of two, replace pow(x,2) with x*x, and use shift instructions for multiplication/division by powers of two.
a = a & 7; // faster than a % 8
x = x * x; // faster than pow(x,2.0)4. Optimize Structure Member Layout
Order members from largest to smallest type to minimize padding, and manually add padding if necessary to align the whole structure to the size of the largest member.
struct {
double x;
long k;
char a[5];
char pad[7]; // manual padding
} baz;5. Loop Optimizations
(1) Unroll Small Loops
When the loop body is tiny, duplicate the body multiple times to reduce loop overhead.
for (i = 0; i < 100; ) {
do_stuff(i); i++;
do_stuff(i); i++;
// … repeated …
}(2) Extract Invariant Computations
Move calculations that do not depend on the loop variable outside the loop.
int factorial_table[] = {1,1,2,6,24,120,720 /* … */};
for (i = 0; i < MAX; ++i) {
h = 14 * i; // moved outside if possible
printf("%d", h);
}(3) Prefer Decrementing Loops
Using a decrementing counter often yields shorter assembly because many CPUs have a zero‑test instruction.
for (i = 1000; i > 0; --i) ;(4) Use Do‑While for Infinite Loops
A for(;;) loop compiles to a single jump, saving a few bytes compared to while(1).
6. Increase CPU Parallelism
Split long dependency chains into independent streams so the processor can execute them in parallel. Example: accumulate four partial sums simultaneously.
double sum1=0,sum2=0,sum3=0,sum4=0;
for (i=0;i<100;i+=4) {
sum1 += a[i];
sum2 += a[i+1];
sum3 += a[i+2];
sum4 += a[i+3];
}
sum = (sum1+sum2)+(sum3+sum4);7. Function Optimizations
(1) Inline Small Functions
Mark frequently called tiny functions as inline to eliminate call overhead.
(2) Declare Unused Return Values as void
Explicitly use void for functions whose result is never used.
(3) Reduce Parameter Count
Prefer global variables over many function arguments when performance outweighs modularity.
8. Use Register Variables Wisely
Declare hot local variables with the register keyword to encourage the compiler to keep them in CPU registers.
9. Prefer Nested if Over Long Chains
Break long conditional lists into nested if statements to avoid unnecessary comparisons.
10. General Advice
Optimization is a balancing act; aggressive speed tricks may reduce readability or increase code size. Apply these techniques selectively, especially in performance‑critical embedded code.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
