Re: Optimizing multiple dispatch

From: Jeff Epler (jepler_at_unpythonic.net)
Date: 06/03/04


Date: Thu, 3 Jun 2004 10:30:40 -0500
To: Jacek Generowicz <jacek.generowicz@cern.ch>


Pyrex is just a translator, there's no dependency on a Pyrex library or
include file when you actually want to compile the generated .c

On Thu, Jun 03, 2004 at 04:55:19PM +0200, Jacek Generowicz wrote:
> Jeff Epler <jepler@unpythonic.net> writes:
>
> > Avoding map() may help:
> > tuple([type(a) for a in args])
>
> Nonononononooooo! :-)
[...]
> > ... hmm, map is faster than listcomp. my mistake!
>
> :-)
>
> I've asked for optimization advice a few times here, and usually the
> responses include "replace map with a list comprehension" ... and yet
> the map is always faster. I wonder where people get this idea that map
> is slow ... until this started happening I had always assumed that
> "everyone" knows that the map/reduce/filter family are the fastest
> Python looping constructs.

No message about optimization should be without one downright wrong
suggestion.

The common wisdom about listcomp speed may apply when the body doesn't
include a function call, but the map version would include a lambda:
    $ timeit 'map(lambda x:x+1, range(100))'
    1000 loops, best of 3: 230 usec per loop
    $ timeit '[x+1 for x in range(100)]'
    10000 loops, best of 3: 156 usec per loop

Jeff






Relevant Pages

  • Re: Can I overload the compare (cmp()) function for a Lists ([]) index function?
    ... 100000 loops, best of 3: 5.05 usec per loop ... Functions like filter() and mapare really only more efficient when you have an existing C-coded function, like ``map``. ... Of course, if the filtercode is clearer to you, feel free to use it, but I find that most folks find list comprehensions easier to read than map() and filtercode. ...
    (comp.lang.python)
  • Re: Integer From A Float List?!?
    ... > For builtin functions, map is usually the fastest option (and, IMO, the ... List comprehensions should be preferred to map + lambda, ... 1000 loops, best of 3: 442 usec per loop ...
    (comp.lang.python)
  • RE: string goes away
    ... > mapat all is for improving the performance. ... Note that you can use `str.upper` in map ... ... 1000 loops, best of 3: 304 usec per loop ...
    (comp.lang.python)
  • Re: string goes away
    ... I read in an guidline for improving Python's performance that you should prefer map() compared to list comprehensions. ... 100000 loops, best of 3: 9.24 usec per loop ...
    (comp.lang.python)
  • Re: reduce()--what is it good for? (was: Re: reduce() anomaly?)
    ... 1000 loops, best of 3: 290 usec per loop ... is about 3 times faster on a typical long seq ... > def longest: ...
    (comp.lang.python)