Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > circular iteration

Reply
Thread Tools

circular iteration

 
 
Flavio codeco coelho
Guest
Posts: n/a
 
      01-21-2005
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
 
Reply With Quote
 
 
 
 
Duncan Booth
Guest
Posts: n/a
 
      01-21-2005
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
>>>

 
Reply With Quote
 
 
 
 
Simon Brunning
Guest
Posts: n/a
 
      01-21-2005
On 21 Jan 2005 08:31:02 -0800, Flavio codeco coelho <(E-Mail Removed)> 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,
http://www.velocityreviews.com/forums/(E-Mail Removed),
http://www.brunningonline.net/simon/blog/
 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      01-21-2005
"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>



 
Reply With Quote
 
Alex Martelli
Guest
Posts: n/a
 
      01-22-2005
Simon Brunning <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Struts - Problem with nested iteration or double iteration Rudi Java 5 10-01-2008 03:30 AM
Showing value of loop iteration in assert statement dwerdna VHDL 5 03-31-2005 05:23 PM
implementing iteration- and conditional-tags with ASP.NET jurujuba ASP .Net 3 02-20-2005 08:16 AM
Semi-circular definitions (plus circular references) Kiuhnm C++ 16 01-03-2005 03:49 AM
looping and displaying progress with every iteration Bruce Whitehouse ASP .Net 3 11-23-2003 07:15 PM



Advertisments