1

My concern is with regards to something like:

for i in range(1000000000):
    ...

From a naive point of view, I assume that an array of 1000000000 integers is allocated and initialized before the loop begins, and deallocated only after the loop ends.

So I am wondering whether or not I should replace it with:

i = 0
while i < 1000000000:
    ...
    i += 1

But according to this answer, a for loop is executed faster than the equivalent while loop.

So do I have to settle for this kind of trade-off, or is my assumption above wrong, and there is no significant memory impact when iterating a large range?

MSeifert
  • 145,886
  • 38
  • 333
  • 352
goodvibration
  • 5,980
  • 4
  • 28
  • 61
  • @mkrieger1: My question is "does range consume memory", not "what is the difference between range and xrange". So **by definition**, these two questions cannot be a duplicate. The two answers might be, so you can mark them duplicate instead. – goodvibration Jul 17 '17 at 12:23
  • @khelwood: My question is "does range consume memory", not "what is the difference between range and xrange". So **by definition**, these two questions cannot be a duplicate. The two answers might be, so you can mark them duplicate instead. – goodvibration Jul 17 '17 at 12:24
  • @Jean-François Fabre: My question is "does range consume memory", not "what is the difference between range and xrange". So **by definition**, these two questions cannot be a duplicate. The two answers might be, so you can mark them duplicate instead. – goodvibration Jul 17 '17 at 12:25
  • *Clears Throat* https://stackoverflow.com/questions/45129146/how-do-i-create-a-for-loop-where-the-variables-value-is-equal-to-the-stop-value – cs95 Jul 17 '17 at 12:32
  • 1
    The irony of doing exact same comment three times. :D – klutt Jul 17 '17 at 12:45

1 Answers1

5

Yes, in python-2.x range will create the whole list.

But you don't necessarily need a while loop you can also use the lazy-range-alternative: xrange.

for i in xrange(1000000000):
    ...
MSeifert
  • 145,886
  • 38
  • 333
  • 352
  • Great, thanks. But wouldn't that ultimately affect performance in the same way that a `while` loop does? – goodvibration Jul 17 '17 at 12:21
  • 1
    @goodvibration: Probably `for i in xrange` will be slightly faster than `while`, but difference will be small. In general for short lists, `range` will be faster than `xrange` but use more memory. It would only be noticeably be faster if you have a situation where you get to re-use the list a lot, because `xrange` will always generate elements on the fly, whilst you can re-use the list from `range` multiple times, it is just a list. At some point the memory use will get to the point that `xrange` is faster regardless. And for *most* purposes the difference is small either way, – Neil Slater Jul 17 '17 at 12:25
  • @goodvibration In my experience `xrange` will be faster than `range` and much faster than `while` for everything except short `range`s. There `range` is a bit faster. – MSeifert Jul 17 '17 at 12:32
  • Thank you very much for the detailed explanation!!! – goodvibration Jul 17 '17 at 12:32
  • @goodvibration I created a [gist](https://gist.github.com/MSeifert04/e739bc60a703d4b9eca6849a80022006) with my timings, you can experiment with that yourself if you like :) – MSeifert Jul 17 '17 at 12:36