Time taken by Loop unrolling vs Normal loop | CPP program to compare normal loops and loops with unrolling technique ; n is 8 lakhs ; t to note start time ; to store the sum ; Normal loop ; to mark end time ; to mark start time of unrolling ; Unrolling technique ( assuming that n is a multiple of 8 ) . ; to mark the end of loop
#include <iostream> NEW_LINE #include <time.h> NEW_LINE 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 ; }