Is it better to use a nested loop for large repetitions, or just put the entire range in one loop? Which is faster / less difficult?

Which one is better?

for x in range(0,100):
   print("Lorem Ipsum")

      


for x in range(0,10):
    for y in range(0,10):
        print("Lorem Ipsum")

      

+3


source to share


4 answers


The second is harder to read and you create an unnecessary range

iterable (a list

in Python 2, which consumes less memory and creates an object faster range

in Python 3).



An for

unnecessary iterator is created from an unnecessary iterable inner loop (a list_iterator

in Python 2, a range_iterator

in Python 3).

+4


source


The first one is more readable and understandable. Use this.

In terms of performance, I doubt it makes any difference, and if it does, 0-100 is faster because it has less code (unless the double loop is optimized) and therefore a shorter code path.



When in doubt about such things, use the one that is easier to understand when reading the code. Premature optimization is a sin.

+1


source


You can use the dis

from module dis

to parse and parse the bytecode that one of your loops is best suited for (thus your loops need less memory, fewer iterators, etc.).

Here's the trace:

from dis import dis
def loop1():
    for x in range(100):
        pass

def loop2():
    for x in range(10):
        for j in range(10):
            pass

      

Now look under the hood of each cycle:

dis(loop1)
  2           0 SETUP_LOOP              20 (to 23)
              3 LOAD_GLOBAL              0 (range)
              6 LOAD_CONST               1 (100)
              9 CALL_FUNCTION            1 (1 positional, 0 keyword pair)
             12 GET_ITER
        >>   13 FOR_ITER                 6 (to 22)
             16 STORE_FAST               0 (x)

  3          19 JUMP_ABSOLUTE           13
        >>   22 POP_BLOCK
        >>   23 LOAD_CONST               0 (None)
             26 RETURN_VALUE

      

And look at the amount of data and operations required in the second loop:

dis(loop2)
  2           0 SETUP_LOOP              43 (to 46)
              3 LOAD_GLOBAL              0 (range)
              6 LOAD_CONST               1 (10)
              9 CALL_FUNCTION            1 (1 positional, 0 keyword pair)
             12 GET_ITER
        >>   13 FOR_ITER                29 (to 45)
             16 STORE_FAST               0 (x)

  3          19 SETUP_LOOP              20 (to 42)
             22 LOAD_GLOBAL              0 (range)
             25 LOAD_CONST               1 (10)
             28 CALL_FUNCTION            1 (1 positional, 0 keyword pair)
             31 GET_ITER
        >>   32 FOR_ITER                 6 (to 41)
             35 STORE_FAST               1 (j)

  4          38 JUMP_ABSOLUTE           32
        >>   41 POP_BLOCK
        >>   42 JUMP_ABSOLUTE           13
        >>   45 POP_BLOCK
        >>   46 LOAD_CONST               0 (None)
             49 RETURN_VALUE

      

Because both loops do the same thing, the first one is much better.

+1


source


Imagine how you would change the nested loop to 101 iterations instead of 100, and the drawback is clear.

0


source







All Articles