Time taken by Loop unrolling vs Normal loop | Java 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
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 ) ) ) ; } }