473,466 Members | 1,382 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

I'm missing something here with range vs. xrange

I've been playing with Python a bit. Doing little performance benchmarks and
working with Psyco. It's been fun and I've been learning a lot. For
example, in a previous post, I was looking for a way to dynamically add new
runtime function to a class. Martin told me to use a class instance
variable instead. It turns out that's faster than hard coding a list of
functions. Thanks Martin.

I read that the range function builds a list and that xrange returns an
iterator and is therefore more efficient. 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? I searched the web but couldn't find
anything that would account for the similarities.
Dec 6 '07 #1
2 2199
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

Dec 6 '07 #2
On Dec 7, 2007 3:08 PM, Joe Goldthwaite <jo*@goldthwaites.comwrote:
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.
You can't imagine why someone might prefer an iterative solution over
a greedy one? Depending on the conditions, the cost of creating the
list can be a greater or a lesser part of the total time spent. Actual
iteration is essentially the same cost for both. Try looking at memory
usage while you're running these tests.

Here's my test results:
C:\>python -m timeit "for x in range(10000000):pass"
10 loops, best of 3: 593 msec per loop

Memory usage (extremely rough, only for comparison purposes: 163 MB

C:\>python -m timeit "for x in xrange(10000000):pass"
10 loops, best of 3: 320 msec per loop

Memory usage: just under 4MB

You mentioned psyco in your original post, which has specific
optimizations for range - I believe it allocates the entire list as an
empty memory block, and then creates the integer objects yielded from
the range lazily.

C:\>python -m timeit "range(10000000)"
10 loops, best of 3: 299 msec per loop

C:\>python -m timeit -s "import psyco;psyco.full()" "range(10000000)"
10 loops, best of 3: 0.0376 usec per loop
Dec 7 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Harald Massa | last post by:
which one should I use? Recommendations? Harald
7
by: Christian Neumann | last post by:
Hello, i have a problem with the built-in function xrange(). Could you by any chance be able to help? I use Python 2.3.4 (final) and i think there is a bug in the built-in
34
by: Blake T. Garretson | last post by:
I want to save some sensitive data (passwords, PIN numbers, etc.) to disk in a secure manner in one of my programs. What is the easiest/best way to accomplish strong file encryption in Python? ...
17
by: John Salerno | last post by:
I'm reading Text Processing in Python right now and I came across a comment that is helping me to see for loops in a new light. I think because I'm used to the C-style for loop where you create a...
29
by: Steve R. Hastings | last post by:
When you compile the expression for i in range(1000): pass does Python make an iterator for range(), and then generate the values on the fly? Or does Python actually allocate the list and...
77
by: Ville Vainio | last post by:
I tried to clear a list today (which I do rather rarely, considering that just doing l = works most of the time) and was shocked, SHOCKED to notice that there is no clear() method. Dicts have it,...
45
by: Summercoolness | last post by:
it seems that range() can be really slow: the following program will run, and the last line shows how long it ran for: import time startTime = time.time() a = 1.0
50
by: John Salerno | last post by:
I know it's popular and very handy, but I'm curious if there are purists out there who think that using something like: for x in range(10): #do something 10 times is unPythonic. The reason I...
0
by: Matthieu Brucher | last post by:
2008/11/5 L V <somelauw@yahoo.com>: Hi, I don't think the Python developers list is th best list to post this kind of question. What version of Python did you use for this test? Matthieu
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.