Professional Documents
Culture Documents
Code Improvement
Ex.
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,
x+0⇒x x–0⇒x
x*1⇒x x/1⇒x
x*0⇒0
Strength reduction
Replace expensive operations
x := x * 8 ⇒ x := x << 3
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
Ex.
x := y; ⇒ s := y * f(y)
s := x * f(x)
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
Ex.
s := 0;
for (i=0; i<n; i++) { s := s + 4; }
s := 0;
while (s < 4*n) { s := s + 4; }
Code Optimization Techniques
Loop fusion
Some adjacent loops can be fused into one loop to reduce
loop overhead and improve run-time performance.
Ex.
for i=1 to N do for i=1 to N do
A[i] = B[i] + 1 A[i] = B[i] + 1
endfor C[i] = A[i] / 2
for i=1 to N do D[i] = 1 / C[i+1]
C[i] = A[i] / 2 endfor
endfor
for i=1 to N do
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.
Ex.
struct
{ char x;
int y; }
Here size of struct is 4 bytes !!!!!
Points to be remember while using Register variable –
Code Optimization Techniques
/* sizeof = 64 bytes */ /*sizeof = 48 bytes */
struct foo sturct foo
{ float a; double b; { double b; double d;
float c; double d; short long f;
e; long h;
long f; float c;
short g; float a;
long h; int j;
char i; int l;
int j; short e;
char k; short g;
int l; }; char k;
char I;};
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 tricks in C
Usages of Register Variables
Use Register variables whenever possible. Accessed
from register rather from memory, so accessing is
faster.
Ex. register float val;
register double dval;
register int ival;
Miscellaneous
Use unsigned int instead of int if we know the value
will never be negative.
Avoid unnecessary division.
Ex: (a / b) > c can be rewritten as a > (c * b)
Use shift operations >> and << instead of integer
multiplication and division, where possible.
Code Optimization tricks in C
Miscellaneous
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
Miscellaneous Continue…..
Use unsigned int instead of int if we know
the value will never be negative.
Avoid unnecessary division.
Phases of Code Improvement