circular iteration

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Flavio codeco coelho

    #1

    circular iteration

    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
  • Duncan Booth

    #2
    Re: circular iteration

    Flavio codeco coelho wrote:
    [color=blue]
    > 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
    >[/color]
    [color=blue][color=green][color=darkred]
    >>> import itertools
    >>> c=['r','g','b','c' ,'m','y','k']
    >>> circ = itertools.cycle (c)
    >>> for i in range(30):[/color][/color][/color]
    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[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    Comment

    • Simon Brunning

      #3
      Re: circular iteration

      On 21 Jan 2005 08:31:02 -0800, Flavio codeco coelho <fccoelho@gmail .com> wrote:[color=blue]
      > 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)][/color]

      I don''t know if it's faster, but:
      [color=blue][color=green][color=darkred]
      >>> import itertools
      >>> c=['r','g','b','c' ,'m','y','k']
      >>> for i in itertools.islic e(itertools.cyc le(c), 30):[/color][/color][/color]
      .... print i

      --
      Cheers,
      Simon B,
      simon@brunningo nline.net,

      Comment

      • Fredrik Lundh

        #4
        Re: circular iteration

        "Flavio codeco coelho" wrote:
        [color=blue]
        > 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)][/color]

        have you benchmarked this, and found it lacking, or are you just trying
        to optimize prematurely?

        </F>



        Comment

        • Alex Martelli

          #5
          Re: circular iteration

          Simon Brunning <simon.brunning @gmail.com> wrote:
          ...[color=blue][color=green]
          > > is there a faster way to build a circular iterator in python that by[/color][/color]
          doing this:[color=blue][color=green]
          > >
          > > c=['r','g','b','c' ,'m','y','k']
          > >
          > > for i in range(30):
          > > print c[i%len(c)][/color]
          >
          > I don''t know if it's faster, but:
          >[color=green][color=darkred]
          > >>> import itertools
          > >>> c=['r','g','b','c' ,'m','y','k']
          > >>> for i in itertools.islic e(itertools.cyc le(c), 30):[/color][/color]
          > ... print i[/color]

          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.cy cle(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

          Comment

          Working...