The examples in this article describe the use of range () and xrange () in Python. Share to everyone for your reference, specific as follows:
Range is said to be more expensive than xrange because range generates a list object directly, and Xrange returns one of its values each time it is invoked (reference: http://www.jb51.net/article/50072.htm). So curiosity did a little test to compare the performance of two functions in the end how much difference.
(1) Test code
#!/usr/bin/env python from
datetime import *
def test_range ():
c1=0
t1 = DateTime.Now () to
I in Range (0,100000):
C1 + 1
print datetime.now ()-t1
def test_xrange (): c1=0 t1
= DateTime.Now () For
i in Xrange (0,100000):
C1 + + 1
print datetime.now ()-t1
if __name__ = ' __main__ ':
test_ Range ()
Test_xrange ()
(2) Operation result
(3) Summary
From the three running results of the above figure, we can see that range is about 70% more than xrange in the same amount of calculation. In addition, in the case of different computational volume, the usage is basically maintained in this proportion. Therefore, Xrange is a good choice if you do not need to return to the list object.
More information about Python-related content can be viewed in this site: "Python file and directory operation tips Summary", "Python text file Operation tips Summary", "Python URL operation tips Summary", "Python Picture Operation tips Summary", " Python data structure and algorithm tutorial, Python socket Programming Tips Summary, Python function Usage tips Summary, python string manipulation tips and Python introductory and advanced classic tutorials
I hope this article will help you with Python programming.