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

后端 未结 28 2074
深忆病人
深忆病人 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:11

    range(x,y) returns a list of each number in between x and y if you use a for loop, then range is slower. In fact, range has a bigger Index range. range(x.y) will print out a list of all the numbers in between x and y

    xrange(x,y) returns xrange(x,y) but if you used a for loop, then xrange is faster. xrange has a smaller Index range. xrange will not only print out xrange(x,y) but it will still keep all the numbers that are in it.

    [In] range(1,10)
    [Out] [1, 2, 3, 4, 5, 6, 7, 8, 9]
    [In] xrange(1,10)
    [Out] xrange(1,10)
    

    If you use a for loop, then it would work

    [In] for i in range(1,10):
            print i
    [Out] 1
          2
          3
          4
          5
          6
          7
          8
          9
    [In] for i in xrange(1,10):
             print i
    [Out] 1
          2
          3
          4
          5
          6
          7
          8
          9
    

    There isn't much difference when using loops, though there is a difference when just printing it!

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

    xrange() and range() in python works similarly as for the user , but the difference comes when we are talking about how the memory is allocated in using both the function.

    When we are using range() we allocate memory for all the variables it is generating, so it is not recommended to use with larger no. of variables to be generated.

    xrange() on the other hand generate only a particular value at a time and can only be used with the for loop to print all the values required.

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

    In python 2.x

    range(x) returns a list, that is created in memory with x elements.

    >>> a = range(5)
    >>> a
    [0, 1, 2, 3, 4]
    

    xrange(x) returns an xrange object which is a generator obj which generates the numbers on demand. they are computed during for-loop(Lazy Evaluation).

    For looping, this is slightly faster than range() and more memory efficient.

    >>> b = xrange(5)
    >>> b
    xrange(5)
    
    0 讨论(0)
  • 2020-11-22 04:14

    Range returns a list while xrange returns an xrange object which takes the same memory irrespective of the range size,as in this case,only one element is generated and available per iteration whereas in case of using range, all the elements are generated at once and are available in the memory.

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

    range(): range(1, 10) returns a list from 1 to 10 numbers & hold whole list in memory.

    xrange(): Like range(), but instead of returning a list, returns an object that generates the numbers in the range on demand. For looping, this is lightly faster than range() and more memory efficient. xrange() object like an iterator and generates the numbers on demand.(Lazy Evaluation)

    In [1]: range(1,10)
    
    Out[1]: [1, 2, 3, 4, 5, 6, 7, 8, 9]
    
    In [2]: xrange(10)
    
    Out[2]: xrange(10)
    
    In [3]: print xrange.__doc__
    
    xrange([start,] stop[, step]) -> xrange object
    
    0 讨论(0)
  • 2020-11-22 04:16

    range creates a list, so if you do range(1, 10000000) it creates a list in memory with 10000000 elements. xrange is a generator, so it evaluates lazily.

    This brings you two advantages:

    1. You can iterate longer lists without getting a MemoryError.
    2. As it resolves each number lazily, if you stop iteration early, you won't waste time creating the whole list.
    0 讨论(0)
提交回复
热议问题