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.
Hello Hello Hello Hello Hello
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.
- Increases program efficiency.
- Reduces loop overhead.
- If statements in loop are not dependent on each other, they can be executed in parallel.
- 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.
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 email@example.com. 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.
- Simplified International Data Encryption Algorithm (IDEA)
- How is the time complexity of Sieve of Eratosthenes is n*log(log(n))?
- Diamond operator for Anonymous Inner Class with Examples in Java
- Make all the array elements odd with minimum operations of given type
- Divide N into K unique parts such that gcd of those parts is maximum
- Maximize the number of palindromic Strings
- Find the smallest subarray having atleast one duplicate
- Find the minimum cost to cross the River
- Find last two remaining elements after removing median of any 3 consecutive elements repeatedly
- Maximum XOR value of maximum and second maximum element among all possible subarrays
- Longest sub-sequence of a binary string divisible by 3
- Minimum cost to traverse from one index to another in the String
- Minimum swaps required to make a binary string divisible by 2^k
- Number of ways to convert a character X to a string Y