VimalSheoran
VimalSheoran

Reputation: 55

Is it better to use nested loop for bigger repetitions or just put the entire range into one loop? Which is faster / less complex?

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")

Upvotes: 3

Views: 71

Answers (4)

VPfB
VPfB

Reputation: 17352

Just imagine how would you modify the nested loop for 101 iterations instead of 100 and the disadvantage is clear.

Upvotes: 0

Chiheb Nexus
Chiheb Nexus

Reputation: 9267

You can use dis from dis module to disassemble and analyse the bytecode of wich one of your loops is better (in a way your loops needs less memory, less iterators, etc ...).

Here is a traceback:

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 loop:

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 needed in your 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 of loops do the same thing, the first one is a far better.

Upvotes: 1

timgeb
timgeb

Reputation: 78780

The second one is harder to read and you construct an unnecessary range iterable (a list in Python 2, a less memory consuming and faster to create range object in Python 3).

From the unnecessary iterable the inner for loop constructs an unnecessary iterator (a list_iterator in Python 2, a range_iterator in Python 3).

Upvotes: 4

Rahkiin
Rahkiin

Reputation: 11

The first one is more readable and easier understandable. Use that.

Regarding performance, I doubt it makes any difference and if it does, the 0-100 is faster, because it has smaller code (if the double loop is not optimized away) and thus a smaller code path.

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

Upvotes: 1

Related Questions