We have discussed loop unrolling. The idea is to increase performance by grouping loop statements so that there are less number of loop control instruction and loop test instructions
C++
#include <iostream>
#include <time.h>
using namespace std;
int main() {
int n = 800000;
clock_t t = clock ();
long long int sum = 0;
for ( int i = 1; i <= n; i++)
sum += i;
t = clock () - t;
cout << "sum is: " << sum << endl;
cout << "time taken by normal loops:"
"(float)t / CLOCKS_PER_SEC << " seconds"
<< endl;
t = clock ();
sum = 0;
for ( int i = 1; i <= n; i += 8) {
sum += i sum += (i + 1);
sum += (i + 2);
sum += (i + 3);
sum += (i + 4);
sum += (i + 5);
sum += (i + 6);
sum += (i + 7);
}
t = clock () - t;
cout << "Sum is: " << sum << endl;
cout << "Time taken by unrolling: "
"(float)t / CLOCKS_PER_SEC << " seconds";
return 0;
}
|
Java
class GFG
{
public static void main(String[] args)
{
int n = 800000 ;
double t = ( double )System.nanoTime();
long sum = 0 ;
for ( int i = 1 ; i <= n; i++)
sum += i;
t = ( double )System.nanoTime() - t;
System.out.println( "sum is: " +
Double.toString(sum));
System.out.println( "time taken by normal loops:" +
Double.toString(t / Math.pow( 10.0 , 9.0 )));
t = ( double )System.nanoTime();
sum = 0 ;
for ( int i = 1 ; i <= n; i += 8 )
{
sum += i ;
sum += (i + 1 );
sum += (i + 2 );
sum += (i + 3 );
sum += (i + 4 );
sum += (i + 5 );
sum += (i + 6 );
sum += (i + 7 );
}
t = ( double )System.nanoTime() - t;
System.out.println( "sum is: " +
Double.toString(sum));
System.out.println( "time taken by normal loops:" +
Double.toString(t / Math.pow( 10.0 , 9.0 )));
}
}
|
Python3
from timeit import default_timer as clock
if __name__ = = "__main__" :
n = 800000 ;
t = clock()
sum = 0
for i in range ( 1 , n + 1 ):
sum + = i
t = clock() - t
print ( "sum is: " + str ( sum ))
print ( "time taken by normal " +
"loops:" + str (t))
t = clock()
sum = 0
for i in range ( 1 , n + 1 , 8 ):
sum + = i
sum + = (i + 1 )
sum + = (i + 2 )
sum + = (i + 3 )
sum + = (i + 4 )
sum + = (i + 5 )
sum + = (i + 6 )
sum + = (i + 7 )
t = clock() - t
print ( "Sum is: " + str ( sum ))
print ( "Time taken by unrolling: " +
str (t))
|
C#
using System;
using System.Diagnostics;
class GFG
{
private static double nanoTime()
{
long nano = 10000L * Stopwatch.GetTimestamp();
nano /= TimeSpan.TicksPerMillisecond;
nano *= 100L;
return nano;
}
public static void Main(String[] args)
{
int n = 800000;
double t = nanoTime();
long sum = 0;
for ( int i = 1; i <= n; i++)
sum += i;
t = nanoTime() - t;
Console.WriteLine( "sum is: " + sum);
Console.WriteLine( "time taken by normal loops:" +
(t / Math.Pow(10.0, 9.0)));
t = nanoTime();
sum = 0;
for ( int i = 1; i <= n; i += 8)
{
sum += i ;
sum += (i + 1);
sum += (i + 2);
sum += (i + 3);
sum += (i + 4);
sum += (i + 5);
sum += (i + 6);
sum += (i + 7);
}
t = nanoTime() - t;
Console.WriteLine( "sum is: " + (sum));
Console.WriteLine( "time taken by normal loops:" +
(t / Math.Pow(10.0, 9.0)));
}
}
|
Javascript
<script>
var n = 800000;
var t = new Date();
var sum = 0;
for (let i = 1; i <= n; i++)
sum += i;
t = new Date() - t;
document.write( "sum is: " +(sum));
document.write( "<br/>time taken by normal loops:" +
(t / Math.pow(10.0, 9.0)).toFixed(10));
t = new Date();
sum = 0;
for (i = 1; i <= n; i += 8) {
sum += i;
sum += (i + 1);
sum += (i + 2);
sum += (i + 3);
sum += (i + 4);
sum += (i + 5);
sum += (i + 6);
sum += (i + 7);
}
t = new Date() - t;
document.write( "<br/>sum is: " + (sum));
document.write( "<br/>time taken by normal loops:" + (t / Math.pow(10.0, 9.0)).toFixed(10));
</script>
|
Output:
Sum is: 320000400000
Time taken: 0.002645 seconds
Sum is: 320000400000
Time taken: 0.001799 seconds
Please refer loop unrolling for comparison of normal loops and loop unrolling.
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!