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
On 2006-07-20, Paul Boddie <pa**@boddie.or g.ukwrote:
Alex Martelli wrote:
>Paul Boddie <pa**@boddie.or g.ukwrote:
>
Well, range is a function in the current implementation, although its
usage is similar to that one would get if it were a class, particularly
a subclass of list or one providing a list-style interface. With such a
class, you could provide a __contains__ method which could answer the
question of what the range contains based on the semantics guaranteed
by a range (in contrast to a normal list).
You'd also have to override just about every mutating method to switch back to a "normal" __contains__ (or change self's type on the fly) -- a pretty heavy price to pay.
A subclass of list is probably a bad idea in hindsight, due to various
probable requirements of it actually needing to be a list with all its
contents, whereas we wanted to avoid having anything like a list around
until the contents of this "lazy list" were required by the program. If
we really wanted to subclass something, we could consider subclassing
the slice class/type, but that isn't subclassable in today's Python for
some reason, and it doesn't really provide anything substantial,
anyway. However, Python being the language it is, an appropriately
behaving class is quite easily written from scratch.
Except that if you write your own class from scratch, you can't use
it as a slice. For a language that is supposed to be about duck typing
I find it strange that if I make my own class with a start, stop and
step attribute, that python barfs on it when I want to use it as a
slice.
>>class sl(object):
.... def __init__(self, start = None, stop = None, step = None):
.... self.start = start
.... self.stop = stop
.... self.step = step
....
>>lst = range(20) s1 = slice(3,13) s2 = sl(3,13) s1.start
3
>>s2.start
3
>>s1.stop
13
>>s2.stop
13
>>s1.step s2.step lst[s1]
[3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
>>lst[s2]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: list indices must be integers
>>>
--
Antoon Pardon
Antoon Pardon wrote:
>
[Subclasses of list or slice for ranges]
Except that if you write your own class from scratch, you can't use
it as a slice. For a language that is supposed to be about duck typing
I find it strange that if I make my own class with a start, stop and
step attribute, that python barfs on it when I want to use it as a
slice.
[s2 has start, stop, step...]
>lst[s2]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: list indices must be integers
In fact, the duck typing seems only to really work outside the
interpreter and the various accompanying built-in classes. Consider a
class similar to the one you defined with the name myslice (for
clarity) and with the apparently necessary indices method; consider it
used as follows:
>>range(0, 10)[myslice(1, 5, 2)]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: list indices must be integers
Here, we start to believe that only traditional index or slice notation
will work. However, we did come across the built-in slice class before;
consider this:
>>range(0, 10)[slice(1, 5, 2)]
[1, 3]
Regardless of whether myslice inherits from object or not, there's no
persuading the interpreter that it is a genuine slice, and remember
that we can't subclass slice (for some reason unknown). So, it would
appear that the interpreter really wants instances from some specific
set of types (presumably discoverable by looking at list_subscript in
listobject.c) rather than some objects conforming to some interface or
protocol, and perhaps it is implemented this way for performance
reasons.
In any case, in the core of Python some types/classes are more equal
than others, and for whatever reason the duck typing breaks down - a
case of "malbik endar" [1] that you just have to be aware of, I
suppose.
Paul
[1] http://www.sciamanna.com/island/pop/...lbik_endar.htm
Paul Boddie wrote:
[...]
Regardless of whether myslice inherits from object or not, there's no
persuading the interpreter that it is a genuine slice, and remember
that we can't subclass slice (for some reason unknown). So, it would
appear that the interpreter really wants instances from some specific
set of types (presumably discoverable by looking at list_subscript in
listobject.c) rather than some objects conforming to some interface or
protocol, and perhaps it is implemented this way for performance
reasons.
In any case, in the core of Python some types/classes are more equal
than others, and for whatever reason the duck typing breaks down - a
case of "malbik endar" [1] that you just have to be aware of, I
suppose.
>>class mySlice(types.S liceType):
... pass
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Error when calling the metaclass bases
type 'slice' is not an acceptable base type
>>>
Indeed.
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
On 2006-07-21, Paul Boddie <pa**@boddie.or g.ukwrote:
Regardless of whether myslice inherits from object or not, there's no
persuading the interpreter that it is a genuine slice, and remember
that we can't subclass slice (for some reason unknown). So, it would
appear that the interpreter really wants instances from some specific
set of types (presumably discoverable by looking at list_subscript in
listobject.c) rather than some objects conforming to some interface or
protocol, and perhaps it is implemented this way for performance
reasons.
In any case, in the core of Python some types/classes are more equal
than others, and for whatever reason the duck typing breaks down - a
case of "malbik endar" [1] that you just have to be aware of, I
suppose.
Is there any chance this will iever change?
Is there any chance the start:stop:step notation will ever
be considered an atom?
--
Antoon Pardon
Antoon Pardon wrote:
>
Except that if you write your own class from scratch, you can't use
it as a slice.
Correct, but we were actually discussing subclassing built-in classes
for use as a replacement for range/xrange. :-)
It may be "hard work" writing all those methods in a totally new
range/xrange class, but passing objects of that class around should
prove satisfactory for the use of most programs. I personally doubt
that it is that much hard work, especially if you stick to a reasonable
selection of list capabilities, for example, rather than attempting to
emulate support for every dodgy trick available to the programmer in
the modern CPython arsenal.
For a language that is supposed to be about duck typing
I find it strange that if I make my own class with a start, stop and
step attribute, that python barfs on it when I want to use it as a
slice.
Yes, my post showed this and gave a reference to where in the CPython
source code the tests for specific types are performed.
Paul
On 2006-07-23, Paul Boddie <pa**@boddie.or g.ukwrote:
Antoon Pardon wrote:
>> Except that if you write your own class from scratch, you can't use it as a slice.
Correct, but we were actually discussing subclassing built-in classes
for use as a replacement for range/xrange. :-)
I think a slice could be very usefull for that.
To me it feel very natural that a slice would be iterable and
could be used in a form like:
for i in slice(10,20):
or if the slice notation would be usable
for i in (10:20):
And why not make a slice indexable too?
Whether slices really are the tool to use here or not
I don know for sure. But the problem is, that you
can't easily experiment with this idea because slices
are not subclassable.
It may be "hard work" writing all those methods in a totally new
range/xrange class, but passing objects of that class around should
prove satisfactory for the use of most programs.
But the parameters you will have to give such a class are the same
you have to pass to a slice. IMO that means that you should at least
consider giving slices this functionality. At least it should be
easy to convert from one to the other and back.
--
Antoon Pardon This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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:
|
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,
|
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);
|
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,
|
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";
| |
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...
|
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))
|
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...
|
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,...
|
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...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
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,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |