Time taken by Loop unrolling vs Normal loop | Python 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
from timeit import default_timer as clock NEW_LINE if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 800000 ; NEW_LINE t = clock ( ) NEW_LINE sum = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT sum += i NEW_LINE DEDENT t = clock ( ) - t NEW_LINE print ( " sum ▁ is : ▁ " + str ( sum ) ) NEW_LINE print ( " time ▁ taken ▁ by ▁ normal ▁ " + " loops : " + str ( t ) ) NEW_LINE t = clock ( ) NEW_LINE sum = 0 NEW_LINE for i in range ( 1 , n + 1 , 8 ) : NEW_LINE INDENT sum += i NEW_LINE sum += ( i + 1 ) NEW_LINE sum += ( i + 2 ) NEW_LINE sum += ( i + 3 ) NEW_LINE sum += ( i + 4 ) NEW_LINE sum += ( i + 5 ) NEW_LINE sum += ( i + 6 ) NEW_LINE sum += ( i + 7 ) NEW_LINE DEDENT t = clock ( ) - t NEW_LINE print ( " Sum ▁ is : ▁ " + str ( sum ) ) NEW_LINE print ( " Time ▁ taken ▁ by ▁ unrolling : ▁ " + str ( t ) ) NEW_LINE DEDENT