Why is a big loop within a small loop faster than a small loop within a big one?

后端 未结 1 1720
梦如初夏
梦如初夏 2021-01-13 04:53

This is my Perl code

$big=10_000_000;
#A:big loop outside
my $begin_time = time;
foreach my $i (1..$big) {
        f         


        
相关标签:
1条回答
  • 2021-01-13 05:32

    There's a certain amount of overhead in executing the inner loop (initialising the variable; making the checks to see if it should end) and in the first case, you are losing this overhead 10,000,000 times; in the second, you are only doing it 10 times.

    EDIT: Let s be the time to setup a loop (e.g. initialise a variable) and i the time to iterate a loop (e.g. test the end condition). Then:

    Big Inner Loop

    T = s1 + 10 * ( i1 + s2 + 10,000,000*i2 )
      = s1 + 10*i1 + 10*s2 + 100,000,000*i2
    

    Big Outer Loop

    T = s1 + 10,000,000 * ( i1 + s2 + 10*i2 )
      = s1 + 10,000,000*i1 + 10,000,000*s2 + 100,000,000*i2
    

    Difference

    diff = 9,999,990*i1 + 9,999,990*s2
    

    So the iteration time of the outer loop (i1) and the set-up time of the inner-loop (s2) are both performed 9,999,990 times more with the big outer loop than with the big inner loop.

    0 讨论(0)
提交回复
热议问题