In python, range () and xrange () Usage Analysis, pythonxrange
This article describes the usage of range () and xrange () in python. We will share this with you for your reference. The details are as follows:
It is said that range is more overhead than xrange because range directly generates a list object, and xrange returns a value in each call (refer to: http://www.bkjia.com/article/50072.htm ). So curious, I made a small test to compare the performance of the two functions.
(1) test code
#! /Usr/bin/env pythonfrom datetime import * def test_range (): c1 = 0 t1 = datetime. now () for I in range (0,100000): c1 + = 1 print datetime. now ()-t1def test_xrange (): c1 = 0 t1 = datetime. now () for I in xrange (0,100000): c1 + = 1 print datetime. now ()-t1if _ name _ = '_ main _': test_range () test_xrange ()
(2) running result
(3) Summary
From the three running results, we can see that range is about 70% more than xrange in the same amount of computing. In addition, the ratio is basically maintained in different computing scenarios. Therefore, xrange is a good choice if you do not need to return a list object.