What is the difference between range and xrange functions in Python 2.X?

后端 未结 28 2043
深忆病人
深忆病人 2020-11-22 03:14

Apparently xrange is faster but I have no idea why it\'s faster (and no proof besides the anecdotal so far that it is faster) or what besides that is different about

相关标签:
28条回答
  • 2020-11-22 04:03

    range creates a list, so if you do range(1, 10000000) it creates a list in memory with 9999999 elements.

    xrange is a generator, so it is a sequence object is a that evaluates lazily.

    This is true, but in Python 3, range() will be implemented by the Python 2 xrange(). If you need to actually generate the list, you will need to do:

    list(range(1,100))
    
    0 讨论(0)
  • 2020-11-22 04:03

    xrange returns an iterator and only keeps one number in memory at a time. range keeps the entire list of numbers in memory.

    0 讨论(0)
  • 2020-11-22 04:04

    xrange only stores the range params and generates the numbers on demand. However the C implementation of Python currently restricts its args to C longs:

    xrange(2**32-1, 2**32+1)  # When long is 32 bits, OverflowError: Python int too large to convert to C long
    range(2**32-1, 2**32+1)   # OK --> [4294967295L, 4294967296L]
    

    Note that in Python 3.0 there is only range and it behaves like the 2.x xrange but without the limitations on minimum and maximum end points.

    0 讨论(0)
  • 2020-11-22 04:06

    In Python 2.x:

    • range creates a list, so if you do range(1, 10000000) it creates a list in memory with 9999999 elements.

    • xrange is a sequence object that evaluates lazily.

    In Python 3, range does the equivalent of python's xrange, and to get the list, you have to use list(range(...)).

    0 讨论(0)
  • 2020-11-22 04:06

    Everyone has explained it greatly. But I wanted it to see it for myself. I use python3. So, I opened the resource monitor (in Windows!), and first, executed the following command first:

    a=0
    for i in range(1,100000):
        a=a+i
    

    and then checked the change in 'In Use' memory. It was insignificant. Then, I ran the following code:

    for i in list(range(1,100000)):
        a=a+i
    

    And it took a big chunk of the memory for use, instantly. And, I was convinced. You can try it for yourself.

    If you are using Python 2X, then replace 'range()' with 'xrange()' in the first code and 'list(range())' with 'range()'.

    0 讨论(0)
  • 2020-11-22 04:07

    I am shocked nobody read doc:

    This function is very similar to range(), but returns an xrange object instead of a list. This is an opaque sequence type which yields the same values as the corresponding list, without actually storing them all simultaneously. The advantage of xrange() over range() is minimal (since xrange() still has to create the values when asked for them) except when a very large range is used on a memory-starved machine or when all of the range’s elements are never used (such as when the loop is usually terminated with break).

    0 讨论(0)
提交回复
热议问题