0% found this document useful (0 votes)
13 views

Loop Optimization

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Loop Optimization

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Loop Optimization is the process of increasing execution speed and reducing the

overheads associated with loops. It plays an important role in improving cache


performance and making effective use of parallel processing capabilities. Most
execution time of a scientific program is spent on loops.

Code Motion (Frequency Reduction)

In frequency reduction, the amount of code in the loop is decreased. A


statement or expression, which can be moved outside the loop body
without affecting the semantics of the program, is moved outside the
loop.
Example:
Before optimization:
while(i<100)
{
a = Sin(x)/Cos(x) + i;
i++;
}

After optimization:

t = Sin(x)/Cos(x);
while(i<100)
{
a = t + i;
i++;
}

2. Induction Variable Elimination

If the value of any variable in any loop gets changed every time, then such
a variable is known as an induction variable. With each iteration, its value
either gets incremented or decremented by some constant value.
Example:
Before optimization:
B1
i:= i+1
x:= 3*i
y:= a[x]
if y< 15, goto B2
In the above example, i and x are locked, if i is incremented by 1 then x is
incremented by 3. So, i and x are induction variables.
After optimization:
B1
i:= i+1
x:= x+4
y:= a[x]
if y< 15, goto B2

3. Strength Reduction

Strength reduction deals with replacing expensive operations with


cheaper ones like multiplication is costlier than addition, so multiplication
can be replaced by addition in the loop.
Example:
Before optimization:
while (x<10)
{
y := 3 * x+1;
a[y] := a[y]-2;
x := x+2;
}
After optimization:
t= 3 * x+1;
while (x<10)
{
y=t;
a[y]= a[y]-2;
x=x+2;
t=t+6;
}

4. Loop Invariant Method

In the loop invariant method, the expression with computation is avoided


inside the loop. That computation is performed outside the loop as
computing the same expression each time was overhead to the system,
and this reduces computation overhead and hence optimizes the code.
Example:
Before optimization:
for (int i=0; i<10;i++)
t= i+(x/y);
...
end;

After optimization:
s = x/y;
for (int i=0; i<10;i++)
t= i+ s;
...
end;
5. Loop Unrolling

Loop unrolling is a loop transformation technique that helps to optimize


the execution time of a program. We basically remove or reduce
iterations. Loop unrolling increases the program’s speed by eliminating
loop control instruction and loop test instructions.
Example:
Before optimization:

for (int i=0; i<5; i++)


printf("Pankaj\n");

After optimization:

printf("Pankaj\n");
printf("Pankaj\n");
printf("Pankaj\n");
printf("Pankaj\n");
printf("Pankaj\n");

6. Loop Jamming

Loop jamming is combining two or more loops in a single loop. It reduces


the time taken to compile the many loops.
Example:
Before optimization:

for(int i=0; i<5; i++)


a = i + 5;
for(int i=0; i<5; i++)
b = i + 10;

After optimization:

for(int i=0; i<5; i++)


{
a = i + 5;
b = i + 10;
}

Machine Dependent and Machine Independent Code Optimization:


Machine Dependent Code Optimization Machine Independent Code
Optimization

Machine independent code


Machine dependent code optimization refers to
optimization refers to optimizing
optimizing code specifically for a particular type
code for use on any type of
of computer or hardware platform
hardware.

The purpose of machine


The purpose of machine dependent code
independent code optimization is to
optimization is to take advantage of specific
make the code as efficient as
features or characteristics of a particular
possible across all hardware
hardware platform.
platforms.

Machine independent code


Machine dependent code optimization is limited
optimization is applicable to any
to a specific hardware platform.
hardware platform.

Machine independent code


Machine dependent code optimization may
optimization produces code that is
result in code that is incompatible with other
compatible with all hardware
hardware platforms
platforms

Machine dependent code optimization may


Machine independent code
make it more difficult to port the code to other
optimization makes it easier to port
hardware platforms.Machine-independent code
the code to other platforms.
optimization aims

Machine dependent code optimization may


Machine independent code
require more maintenance and updates to keep
optimization requires fewer updates
the code optimized for different hardware
and is easier to maintain.
platforms.

Machine dependent code optimization may Machine independent code


take longer to develop, as it requires specialized optimization can be developed
knowledge of the hardware platform and may
more quickly.
require testing on multiple platforms.

Machine independent code


Machine dependent code optimization may
optimization may result in more
result in better performance on the specific
consistent performance across all
hardware platform.
platforms.

Machine dependent code optimization may


Machine independent code
require more complex code, as it needs to take
optimization can use simpler, more
into account the specific features of the
generic code.
hardware platform.

Machine independent code


Machine dependent code optimization may not
optimization can be easily reused on
be reusable on other hardware platforms.
any platform.

You might also like