xrange question
Paul Prescod
paulp at ActiveState.com
Mon May 7 03:45:51 EDT 2001
John Flynn wrote:
>
>...
>
> Yep, I see your point about the clarity of for-loops.
>
> Out of curiosity, at what point do you find that 'xrange' becomes most
> useful? Do you have a rule of thumb, eg. more than, say, 500 iterations uses
> an 'xrange' instead of a range? Or does xrange _replace_ range for you in
> for-loops?
No, xrange is much slower for small loops so I don't replace all ranges
with xranges. Range is also more idiomatic. I think on my machine xrange
starts to "win" around 2000. But allocating even a million items in a
regular range is still quite fast:
>>> start = time.time() ; a=range(1000000) ; print time.time() - start
0.30999994278
So even at a million either one is fine on modern machines. Plus,
consider that if you make a range of a million items you are probably
actually doing something a million times and the construction of the
range list is hardly going to be your bottleneck.
I wouldn't usually think twice to spend a third of a second getting
ready to process a million lines of text or a million patient records or
whatever...
Here's a program you can use to get a feeling for the behavior of range
and xrange:
import time
def testfunc(func, size):
# always do a million iterations, but do it in 100-element chunks,
or
# 1000-element chunks etc.
# I always want to do a million iterations to get a good sense for
# the performance of the function I'm using.
loopfactor = 1000000/float(size)
start = time.time()
for i in xrange(loopfactor):
for i in func(size):
pass
return time.time() - start
for i in [1, 100, 1000, 2000, 5000, 10000, 100000, 1000000]:
print i, "iterations"
print "Range time: ", testfunc(range, i)
print "XRange time: ", testfunc(xrange, i)
print
--
Take a recipe. Leave a recipe.
Python Cookbook! http://www.ActiveState.com/pythoncookbook
More information about the Python-list
mailing list