Range
Function Description: Range ([Start,] stop[, step]), which generates a sequence based on the range specified by start and stop and step set by step.
Range Example:
>>> Range (6) [0, 1, 2, 3, 4, 5]>>> Range (0,6,2) [0, 2, 4]
Xrange
Function Description: Syntactically and range exactly the same, the difference is not an array is generated, but a generator.
xrange Example:
>>> xrange (6) xrange (6) >>> list (xrange (6)) [0, 1, 2, 3, 4, 5]>>> xrange (0,6,2) xrange (0, 6, 2) & Gt;>> list (xrange (0,6,2)) [0, 2, 4]
Range and xrange are used more in the loop, and the following is an analysis of their respective performance in the loop:
If the loop is in range, we can see from the above result that an array will be generated. If the number of elements is less acceptable, but if the number of elements is very large, it is not to open up a lot of memory to store this array? This pair makes memory space for Alexander.
If you use Xrange,xrange to return a generator, you can return one value at a time, so you don't have to open up so much memory space.
So, try to use xrange in the loop, as the number of elements increases, xrange performance is much better than range.
Note that in python3.x, xrange is abolished, and the range function is the same as xrange.
Python's range and xrange