# 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.

**Program 1:**

`// This program does not uses loop unrolling. ` `#include<stdio.h> ` ` ` `int` `main(` `void` `) ` `{ ` ` ` `for` `(` `int` `i=0; i<5; i++) ` ` ` `printf` `(` `"Hello\n"` `); ` `//print hello 5 times ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Program 2:**

`// This program uses loop unrolling. ` `#include<stdio.h> ` ` ` `int` `main(` `void` `) ` `{ ` ` ` `// unrolled the for loop in program 1 ` ` ` `printf` `(` `"Hello\n"` `); ` ` ` `printf` `(` `"Hello\n"` `); ` ` ` `printf` `(` `"Hello\n"` `); ` ` ` `printf` `(` `"Hello\n"` `); ` ` ` `printf` `(` `"Hello\n"` `); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

Hello Hello Hello Hello Hello

**Illustration:**

Program 2 is more efficient than program 1 because in program 1 there is a need to check the value of i and increment the value of i every time round the loop. So small loops like this or loops where there is fixed number of iterations are involved can be unrolled completely to reduce the loop overhead.

**Advantages:**

- Increases program efficiency.
- Reduces loop overhead.
- If statements in loop are not dependent on each other, they can be executed in parallel.

**Disadvantages:**

- Increased program code size, which can be undesirable.
- Possible increased usage of register in a single iteration to store temporary variables which may reduce performance.
- Apart from very small and simple codes, unrolled loops that contain branches are even slower than recursions.

**Reference:**

https://en.wikipedia.org/wiki/Loop_unrolling

This article is contributed by **Harsh Agarwal**. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Number of factors of very large number N modulo M where M is any prime number
- Why Prim’s and Kruskal's MST algorithm fails for Directed Graph?
- Maximum repeated frequency of characters in a given string
- Maximum number of strings that can be formed with given zeros and ones
- Count of even and odd set bit with array element after XOR with K
- Count ways to change direction of edges such that graph becomes acyclic
- Count ways to reach end from start stone with at most K jumps at each step
- Minimum sum of product of elements of pairs of the given array
- Check if N can be expressed as product of 3 distinct numbers
- Find all Factors of Large Perfect Square Natural Number in O(sqrt(sqrt(N))
- Check if all Prime factors of number N are unique or not
- Minimum value of distance of farthest node in a Graph
- Longest palindromic string possible after removal of a substring
- Dixon's Factorization Method with implementation