473,785 Members | 2,423 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

range() is not the best way to check range?

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
for i in range(0, 30000):
if i in range (0, 10000):
a += 1
if not i % 1000: print i

print a, " ", round(time.time () - startTime, 1), "seconds"
---------------------------------
the last line of output is
---------------------------------

10001.0 22.8 seconds

so if i change the line

if i in range (0, 10000):

to

if i >= 0 and i < 10000:

the the last line is

10001.0 0.2 seconds

so approximately, the program ran 100 times faster!

or is there an alternative use of range() or something similar that can
be as fast?

Jul 18 '06
45 8576
Grant Edwards wrote:
On 2006-07-18, Marc 'BlackJack' Rintsch <bj****@gmx.net wrote:
>>In <11************ **********@h48g 2000cwc.googleg roups.com>, tac-tics
wrote:

>>>Grant Edwards wrote:

for pete's sake use the comparison operator like god intended.

if 0 <= i <= 10000:

I'm assuming you used Python's compound comparison as opposed to the
C-style of and'ing two comparisons together to emphasize the fact it is
god's chosen way of doing this ;-)

Pete doesn't like to be called god in public. ;-)


Interesting point. Does the phrase "for pete's sake as god
intended" equate pete with god?

It's possible that pete is not god and yet god's intentions are
in pete's best interest, so something could be "for pete's
sake" and "as god intended" without pete being god.

That said, "for pete's sake" is probably a just an cleaned up
version of "for god's sake", so I probably did call pete god.
No, actually you called god pete ;-)

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Jul 18 '06 #21

Dan Bishop wrote:
Su************@ gmail.com wrote:
it seems that range() can be really slow:
...
if i in range (0, 10000):

This creates a 10,000-element list and sequentially searches it. Of
course that's gonna be slow.
And you're doing it 30000 times.

Jul 18 '06 #22
Nick Craig-Wood wrote:
>
Sets are pretty fast too, and have the advantage of flexibility in
that you can put any numbers in you like
I know this is self-evident to most of the people reading this, but I
thought it worth pointing out that this is a great way to test
membership in range(lo, hi, step) without doing "the necessary
algebra".

i.e. n in set(xrange(0, 10000, 23)) ...
Peace,
~Simon

Jul 18 '06 #23
Simon Forman wrote:
Nick Craig-Wood wrote:
>>
Sets are pretty fast too, and have the advantage of flexibility in
that you can put any numbers in you like

I know this is self-evident to most of the people reading this, but I
thought it worth pointing out that this is a great way to test
membership in range(lo, hi, step) without doing "the necessary
algebra".

i.e. n in set(xrange(0, 10000, 23)) ...
No, its not. It works, but it works by no means faster than

n in range(0, 10000, 23)

Both need O(n), which is a bit slow compared to

(((n - 15) % 23) == 0 and n >= 15 and n < 10000)

that will run in O(1)

Diez
Jul 18 '06 #24
Diez B. Roggisch wrote:
Simon Forman wrote:
Nick Craig-Wood wrote:
>
Sets are pretty fast too, and have the advantage of flexibility in
that you can put any numbers in you like
I know this is self-evident to most of the people reading this, but I
thought it worth pointing out that this is a great way to test
membership in range(lo, hi, step) without doing "the necessary
algebra".

i.e. n in set(xrange(0, 10000, 23)) ...

No, its not. It works, but it works by no means faster than

n in range(0, 10000, 23)

Both need O(n), which is a bit slow compared to

(((n - 15) % 23) == 0 and n >= 15 and n < 10000)

that will run in O(1)

Diez
You're right, of course. I should have said that if you're testing
such a membership, say, 30000 times AND you (like me) are slightly
rusty on the algebra and prefer to let the computer do it, then you
could use something like:

test_set = set(xrange(0, 10000, 23))

if n in test_set:
...

;-)
~Simon

Jul 18 '06 #25
Grant Edwards wrote:
>
It's unclear what you're referring to as "the range".
The notion of something describing a range of values which can be
expanded to a list or, of relevance here, whose boundaries can be
tested efficiently.
Perhaps you're thinking of a slice? Somethign like

if (0:10000).conta ins(x):
Did you mean...?

(0:10000) # SyntaxError
slice(0, 10000).contains (x) # AttributeError
3 in slice(0, 10000) # TypeError

Something like this might suffice if slice could have a __contains__
method or if people thought of slices as natural things to test
against. Perhaps we could ask the original questioner why they chose to
use range in such a way - it might indicate a background in languages
which encourage the construction of ranges and their use in comparisons
- although being told to RTFM may have scared them off.

Paul

Jul 18 '06 #26
Simon Forman wrote:
Nick Craig-Wood wrote:
>Sets are pretty fast too, and have the advantage of flexibility in
that you can put any numbers in you like
I know this is self-evident to most of the people reading this, but I
thought it worth pointing out that this is a great way to test
membership in range(lo, hi, step) without doing "the necessary
algebra".

i.e. n in set(xrange(0, 10000, 23)) ...
This is very very misleading... here are some timings :

python -mtimeit "n=5000" "n in set(xrange(0,10 000))"
1000 loops, best of 3: 1.32 msec per loop

python -mtimeit "n=5000" "n in xrange(0,10000) "
1000 loops, best of 3: 455 usec per loop

python -mtimeit "n=5000" "0 <= n < 10000"
1000000 loops, best of 3: 0.217 usec per loop

sets are fast only if you create them *once* and use them again and
again. even in that case, the sets use up O(n) memory.

with comparison operators, you don't need extra memory *and* there is no
pre-computation required.

[sreeram;]
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFEvRuPrgn 0plK5qqURAiuQAJ 4g1p7DW5LfNj+ml rXHwwXa6FAwRACd Fpwu
KVnwhHLGp8sfn5J 2qaW1eGA=
=+8z4
-----END PGP SIGNATURE-----

Jul 18 '06 #27
On 2006-07-18, Steve Holden <st***@holdenwe b.comwrote:
>That said, "for pete's sake" is probably a just an cleaned up
version of "for god's sake", so I probably did call pete god.

No, actually you called god pete ;-)
Well that's certainly wrong, because we all know god's name is
Howard.

Our father who art in heaven,
Howard be thy name,
....

--
Grant Edwards grante Yow! .. I have a
at VISION! It's a RANCID
visi.com double-FISHWICH on an
ENRICHED BUN!!
Jul 18 '06 #28
On 2006-07-18, Paul Boddie <pa**@boddie.or g.ukwrote:
>It's unclear what you're referring to as "the range".

The notion of something describing a range of values which can be
expanded to a list or, of relevance here, whose boundaries can be
tested efficiently.
>Perhaps you're thinking of a slice? Somethign like

if (0:10000).conta ins(x):
I didn't mean to imply that would actually work, but I thought
maybe that's what you were proposing.
Did you mean...?

(0:10000) # SyntaxError
slice(0, 10000).contains (x) # AttributeError
3 in slice(0, 10000) # TypeError

Something like this might suffice if slice could have a __contains__
method or if people thought of slices as natural things to test
against.
A slice seems to me to be the obvious way to represent a finite
length algebraic sequence of integers. However, obvioiusness
is in the eye of the beholder since as you point out below to
the OP, a range() was the obvious way to it.
Perhaps we could ask the original questioner why they chose to
use range in such a way - it might indicate a background in
languages which encourage the construction of ranges and their
use in comparisons - although being told to RTFM may have
scared them off.
--
Grant Edwards grante Yow! Did I say I was a
at sardine? Or a bus???
visi.com
Jul 18 '06 #29
Dan Bishop:
xrange already has __contains__. The problem is, it's implemented by a
highly-inefficient sequential search. Why not modify it to merely
check the bounds and (value - start) % step == 0?
I think this is a nice idea.

Bye,
bearophile

Jul 18 '06 #30

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

Similar topics

29
7487
by: Chris Dutrow | last post by:
I searched around on the net for a bit, couldn't find anything though. I would like to find some code for a function where I input A Range Of Integers For example: Function( 1, 100 ); And the function will return me an array holding a random subset of integers in that range of a size that I specify So the Function would Probabaly look something like this:
0
1015
by: illo | last post by:
hello guys... a little but very imposrtant question... I have a broadband connection in my office, and i would like to use this connection also from my house (150 meters from the office)... I bought a router and 2 range extenders of belkin.... With the router everything functions very well,
5
5950
by: sameer_deshpande | last post by:
Hi, I need to create a partition table but the column on which I need to create a partition may not have any logical ranges. So while creating or defining partition function I can not use any range. like CREATE PARTITION FUNCTION my_part_func (NUMERIC(7)) AS RANGE LEFT FOR VALUES (1,100,1000);
3
2852
by: toton | last post by:
Hi, I want ro iterate over a few container class within a range specified, instead of begin & end. How to construct a range class, which takes start & end, and iteration is available within that range only. Itaration may be const, bidiractional, forward or backward. Say I have a vector or other container class, like vector<intvec; and want to return a range class like range(vec.begin()+5,
3
4133
by: Alexander Higgins | last post by:
Hello, I would like to thank everyone for there help in advance. I have form which is using an iframe as a Rich Text Editor. Everything works as expected in IE but I have two issues with Firefox. I am using the following to make the frame editable: tmp=document.getElementById("adeditor").contentWindow.document tmp.designMode="On";
2
2214
by: Joe Goldthwaite | last post by:
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...
10
3021
by: Rafael Cunha de Almeida | last post by:
Hi, I've found several sites on google telling me that I shouldn't use rand() % range+1 and I should, instead, use something like: lowest+int(range*rand()/(RAND_MAX + 1.0))
0
9646
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9483
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10346
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10096
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7504
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6742
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5514
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4055
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 we have to send another system
2
3658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.