I'm missing something here with range vs. xrange
Joe Goldthwaite
joe at goldthwaites.com
Fri Dec 7 16:08:43 EST 2007
Here's the simple benchmark;
start = time.time()
for x in xrange(3):
for y in xrange(10000000):
pass
print 'xRange %s' % (time.time() - start)
start = time.time()
for x in range(3):
for y in range(10000000):
pass
print 'Range %s' % (time.time() - start)
Here's what I get;
xRange 92.5529999733
Range 95.2669999599
Not a lot of difference. Range is slower but not by much. I know that range
builds
a list then iterates through it. I thought that xrange just kept a counter
that was
incremented and returned with each call. No list was ever created. If that's
true
(and I guess it's not), xrange would be much faster than range. It seems
almost
identical. Given the amount of performance difference, I don't see why
xrange even
exists.
P.S. I searched google again to try and find out more about range vs.
xrange and
this thread came up first on the list. That was strange. I thought I'd
found
someone else asking the same question as me until I clicked on the link and
found
out it actually was me.
-----Original Message-----
From: python-list-bounces+joe=goldthwaites.com at python.org
[mailto:python-list-bounces+joe=goldthwaites.com at python.org]On Behalf Of
Bjoern Schliessmann
Sent: Thursday, December 06, 2007 3:33 PM
To: python-list at python.org
Subject: Re: I'm missing something here with range vs. xrange
Joe Goldthwaite wrote:
> I read that the range function builds a list and that xrange
> returns an iterator and is therefore more efficient.
This is generally not true.
> In my testing, they both come out to almost exactly the same
> performance wise. Did something get changed in Python 2.4 to make
> them identical?
No. Try again with a list of length 10000000.
Regards,
Björn
--
BOFH excuse #359:
YOU HAVE AN I/O ERROR -> Incompetent Operator error
--
http://mail.python.org/mailman/listinfo/python-list
More information about the Python-list
mailing list