Jun-13-2017, 11:27 PM
(Jun-01-2017, 09:50 AM)Ofnuts Wrote: In python2Daaamn! That's a factor of half a million!xrange(..)
creates the values when they are needed (ie, when your code asks for the next value...), whilerange()
generates all the possible values at once. No big difference when you are using small ranges, but vital if you are using large ranges, especially if your logic makes it unlikely that you use up all the generated values.
# this requires 4 MB to run: sumOfSquares=0 for n in range(1000000): sumOfSquares+=n*n # this requires 8 bytes to run: sumOfSquares=0 for n in xrange(1000000): sumOfSquares+=n*n
![Shocked Shocked](https://python-forum.io/images/smilies/icon_eek.gif)