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

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

提交回复
热议问题