reduce()--what is it good for? (was: Re: reduce() anomaly?)

Georgy Pruss SEE_AT_THE_END at hotmail.com
Fri Nov 7 20:51:13 EST 2003


"Alex Martelli" <aleax at aleax.it> wrote in message news:SwQqb.108165$e5.3904529 at news1.tin.it...
> Georgy Pruss wrote:
>
> > seq=[1,3,4,7]
> > map( int.__sub__, seq[1:], seq[:-1] ) # nearly twice faster than
> > ....zip.... for long arrays
>
> If this is a race, then let's measure things properly and consider
> a few more alternatives, shall we...?

:) No, it's not a race. I just found the map expression to be clear and elegant.
Fortunatelly, one of the fastest solutions too.

G-:

>
> [alex at lancelot xine-lib-1-rc2]$ python a.py
>   reduce: 100 loops, best of 3: 13.8 msec per loop
>      zip: 100 loops, best of 3: 18 msec per loop
>     izip: 100 loops, best of 3: 7.6 msec per loop
>       w2: 100 loops, best of 3: 7.1 msec per loop
>      wib: 100 loops, best of 3: 12.7 msec per loop
>     loop: 100 loops, best of 3: 8.9 msec per loop
>      map: 100 loops, best of 3: 7.6 msec per loop
>
> itertools.w2 is an experimental addition to itertools which I
> doubt I'll be allowed to put in (gaining less than 10% wrt the
> more general izip is hardly worth a new itertool, sigh).  But,
> apart from that, map and izip are head to head, and the plain
> good old Python-coded loop is next best...!  reduce is slowest.
>
> My code...:
> <...>
>
> Alex
>







More information about the Python-list mailing list