Loop Optimization Techniques | Set 2

Prerequisite – Loop Optimization | Set 1

1. Loop Fission: improves locality of reference –
In this, a loop is broken into multiple loops over the same index range, but each new loop contains only a specific part of the original loop’s body. This can improve locality of reference.

Before optimization:

for(i=0;i<100;i++)                          
{a[i]=…
b[i]=…
}

After optimization:

for(i=0;i<100;i++)
a[i]=…
for(i=0;i<100;i++)
b[i]=…

2. Loop Interchange: improves locality of reference –
In these optimizations exchange inner loops with outer loops. When the loop variables index into an array, such a transformation can improve the locality of reference, depending on the array’s layout.



Before optimization:

for(i=0;i<100;i++)
for(j=0;j<100;j++)
a[j][i]=…

After optimization:

for(j=0;j<100;j++)
for(i=0;i<100;i++)
a[j][i]=…

3. Loop Reversal –
Reverses the order in which values are assigned to the index variable. This can help eliminate dependencies and thus enable other optimizations.

Before optimization:

for(i=0;i<100;i++)
a[99-i]=…  

After optimization:

for(i=99;i>=0;i--)
a[i]=…

4. Loop Unrolling: minimizes tests and jumps but increases code size –
The objective of loop unrolling is to increase a program’s speed by reducing instructions that control the loop, such as pointer arithmetic and “end of loop” test condition on each iteration. To eliminate or reduce this computational overhead, loops can be re-written as a repeated sequence of similar independent statements.

Before optimization:

for(i=0;i<100;i++)
a[i]=…  

After optimization:



for(i=0;i<100;i+=2)
{a[i]=…
a[i+1]=…
}

5. Loop Splitting –
This attempts to simplify a loop or eliminate dependencies by breaking it into multiple loops that have the same bodies but iterate over different portions of the index range.

Before optimization:

for(i=0;i<100;i++)
if(i<50)
a[i]=…
else
b[i]=…

After optimization:

for(i=0;i<50;i++)
a[i]=…
for(;i<100;i++)
b[i]=…

6. Loop Peeling: special case of loop splitting –
A special case of loop splitting is loop peeling, which can simplify a loop with a problematic first iteration by performing that iteration separately before entering the loop.

Before optimization:

for(i=0;i<100;i++)
if(i==0)
a[i]=…
else
b[i]=…

After optimization:

a[0]=…
for(i=1;i<100;i++)
b[i]=…

7. Unswitching –
Moves a conditional from inside a loop to outside of it by duplicating the loop’s body, and placing   a version of it inside each of the if and else clauses of the conditional.

Before optimization:

for(i=0;i<100;i++)
if(x>y)
a[i]=…
else
b[i]=…

After optimization:

if(x>y)
for(i=0;i<100;i++)
a[i]=…
else
for(i=0;i<100;i++)
b[i]=…  

8. Loop Test Replacement: increases possibility of dead code elimination –

Before optimization:

i=0;
val=0;
while(i<100)
{val+=5;
i++;
}

After optimization:

i=0;
val=0;
while(val<500)
{val+=5;
i++;
}

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.