[Tutor] map() and lambda to change class instance attribute (fwd)

Bernard Lebel 3dbernard at gmail.com
Sun May 15 00:28:31 CEST 2005


Thanks Alan, that clears things up quite well.

Bernard


On 5/14/05, Alan Gauld <alan.gauld at freenet.co.uk> wrote:
> > So if I undestand you right, mapping a function with map()
> > when it is a built-in function will/may be faster than a for
> > loop, but if it's a custom function (ie. a def one), it will
> > most likely be slower?
> 
> That's right, a builtin function, including map itself will
> be written in C and so be 'fast'. So we have the trade off
> between a Python for loop calling a function or a C loop
> which additionally has to build a list result. But in the
> builtin case we have C code calling C code which is
> much faster than Python code calling C code.
> 
> So for a function written in Python there will be less
> difference and the bulk of the time is likely to be
> spent in the function calls but for builtins C to C
> could give a significant benefit.
> 
> Which is faster will depend on several other factors including
> what the function returns and how easily that can be packaged
> into a list.
> 
> From my personal experience I wouldn't expect map to be
> much faster than a for loop, if at all. But as Kent says
> you can always profile it and test it if you really need
> speed. The real issue is the map call is more obscure
> than an explicit loop since map() is intended to build
> a list not modify an existing list!
> 
> Alan G.
>


More information about the Tutor mailing list