Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What is xrange?
#6
(Jun-01-2017, 09:50 AM)Ofnuts Wrote: In python2 xrange(..) creates the values when they are needed (ie, when your code asks for the next value...), while range() 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
Daaamn! That's a factor of half a million! Shocked
Reply


Messages In This Thread
What is xrange? - by failedSIGNAL - May-30-2017, 01:50 AM
RE: [SMALL QUESTIONS] - by Larz60+ - May-30-2017, 03:21 AM
RE: [SMALL QUESTIONS] - by failedSIGNAL - May-30-2017, 04:21 AM
RE: [SMALL QUESTIONS] - by buran - May-30-2017, 05:10 AM
RE: What is xrange? - by Ofnuts - Jun-01-2017, 09:50 AM
RE: What is xrange? - by ackmondual - Jun-13-2017, 11:27 PM
RE: What is xrange? - by nilamo - Jul-06-2017, 08:27 PM
RE: What is xrange? - by DeaD_EyE - Jul-07-2017, 09:22 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020