circular iteration

Discussion in 'Python' started by Flavio codeco coelho, Jan 21, 2005.

  1. 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
     
    Flavio codeco coelho, Jan 21, 2005
    #1
    1. Advertising

  2. Flavio codeco coelho

    Duncan Booth Guest

    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
    >>>
     
    Duncan Booth, Jan 21, 2005
    #2
    1. Advertising

  3. On 21 Jan 2005 08:31:02 -0800, 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)]


    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.brunningonline.net/simon/blog/
     
    Simon Brunning, Jan 21, 2005
    #3
  4. "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>
     
    Fredrik Lundh, Jan 21, 2005
    #4
  5. Simon Brunning <> 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
     
    Alex Martelli, Jan 22, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dan C Douglas

    Circular references are not supported

    Dan C Douglas, Aug 13, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    1,435
    Steve C. Orr, MCSD
    Aug 13, 2003
  2. Bill Jones

    Re: Circular References

    Bill Jones, Apr 8, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    441
    =?Utf-8?B?RGF2ZQ==?=
    Apr 8, 2004
  3. =?Utf-8?B?S2VpdGggRi4=?=

    Circular References in Visual Studio

    =?Utf-8?B?S2VpdGggRi4=?=, Aug 6, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    6,381
    Cowboy \(Gregory A. Beamer\) [MVP]
    Aug 6, 2004
  4. Kiuhnm
    Replies:
    16
    Views:
    758
    Jonathan Mcdougall
    Jan 3, 2005
  5. Rudi
    Replies:
    5
    Views:
    5,239
Loading...

Share This Page