hi,
is there a faster way to build a circular iterator in python that by doing this:
c=['r','g','b','c','m','y','k']
for i in range(30):
print c[i%len(c)]
thanks,
Flávio 4 1732
Flavio codeco coelho wrote: hi,
is there a faster way to build a circular iterator in python that by doing this:
c=['r','g','b','c','m','y','k']
for i in range(30): print c[i%len(c)]
thanks,
Flávio import itertools c=['r','g','b','c','m','y','k'] circ = itertools.cycle(c) for i in range(30):
print circ.next(),
r g b c m y k r g b c m y k r g b c m y k r g b c m y k r g
On 21 Jan 2005 08:31:02 -0800, Flavio codeco coelho <fc******@gmail.com> wrote: hi,
is there a faster way to build a circular iterator in python that by doing this:
c=['r','g','b','c','m','y','k']
for i in range(30): print c[i%len(c)]
I don''t know if it's faster, but: import itertools c=['r','g','b','c','m','y','k'] for i in itertools.islice(itertools.cycle(c), 30):
.... print i
--
Cheers,
Simon B, si***@brunningonline.net, http://www.brunningonline.net/simon/blog/
"Flavio codeco coelho" wrote: is there a faster way to build a circular iterator in python that by doing this:
c=['r','g','b','c','m','y','k']
for i in range(30): print c[i%len(c)]
have you benchmarked this, and found it lacking, or are you just trying
to optimize prematurely?
</F>
Simon Brunning <si************@gmail.com> wrote:
... is there a faster way to build a circular iterator in python that by
doing this: c=['r','g','b','c','m','y','k']
for i in range(30): print c[i%len(c)]
I don''t know if it's faster, but: import itertools c=['r','g','b','c','m','y','k'] for i in itertools.islice(itertools.cycle(c), 30):
... print i
Whenever you're using itertools, the smart money's on "yes, it's
faster";-).
E.g., on a slow, old iBook...:
kallisti:~ alex$ python -mtimeit -s'c="rgbcmyk"' 'for i in range(30):
c[i%len(c)]'
10000 loops, best of 3: 47 usec per loop
kallisti:~ alex$ python -mtimeit -s'c="rgbcmyk"; import itertools as it'
'for i in it.islice(it.cycle(c),30): i'
10000 loops, best of 3: 26.4 usec per loop
Of course, if you do add back the print statements they'll take orders
of magnitude more time than the cyclic access, so /F's point on
premature optimization may well be appropriate. But, if you're doing
something VERY speedy with each item you access, maybe roughly halving
the overhead for the cyclic access itself MIGHT be measurable (maybe
not; it IS but a few microseconds, after all).
I like itertools' approach because it's higher-abstraction and more
direct. Its blazing speed is just a trick to sell it to conservative
curmudgeons who don't see abstraction as an intrinsic good -- some of
those are swayed by microseconds;-)
Alex This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Henry Miller |
last post by:
I have the following code (much simplified for this post). Note that
SessionKey uses DataAccess, and DataAccess requires SessionKey in it's
constructor.
Public Class SessionKey
Public...
|
by: ernesto basc?n pantoja |
last post by:
Hi everybody:
I'm implementing a general C++ framework and I have a basic question
about circular dependencies:
I am creating a base class Object, my Object class has a method
defined as:...
|
by: Kiuhnm |
last post by:
Is there an elegant way to deal with semi-circular definitions?
Semi-circular definition:
A { B };
B { *A };
Circular reference:
A { *B };
B { *A };
The problems arise when there are more...
|
by: Earth Worm Jim |
last post by:
I have been able to get simple circular references to be serialized in xml
by using the ImportTypeMapping method on the SoapReflectionImporter class.
But I am unable to serialise circular...
|
by: Frank Rizzo |
last post by:
I have a circular reference between 2 classes in the same project (i.e.
each class refers to the other). The app runs fine and I am seeing no
issues, which kind of surprised me.
Are there any...
|
by: Kenneth Lantrip |
last post by:
Anyone got any ideas as to how this process could be improved for speed?
this is what I have...
Dim j, q As Integer
Dim x(16), y(16) As Byte
x.CopyTo(y, 0) ' shift left circular 24 bits
|
by: Stephen Robertson |
last post by:
We are currently in a dead end with a circular reference issue using
vb.net, and are hoping someone might help us resolve it.
Idea...
We have frmmain calling frmperson (dim f as new frmperson)...
|
by: toton |
last post by:
Hi,
I want a circular buffer or queue like container (queue with array
implementation).
Moreover I want random access over the elements. And addition at tail
and remove from head need to be low...
|
by: barias |
last post by:
Although circular dependencies are something developers should normally
avoid, unfortunately they are very easy to create accidentally between
classes in a VS project (i.e. circular compile-time...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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...
|
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,...
|
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...
|
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...
|
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,...
|
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...
| |