Python3: on removing map, reduce, filter

Steven Bethard steven.bethard at gmail.com
Sun Jan 9 16:45:32 EST 2005


Robert Kern wrote:
> Andrey Tatarinov wrote:
> 
>> anyway list comprehensions are just syntaxic sugar for
>>
>>  >>> for var in list:
>>  >>>     smth = ...
>>  >>>     res.append(smth)
>>
>> (is that correct?)
>>
>> so there will be no speed gain, while map etc. are C-implemented
> 
> 
> It depends.
> 
> Try
> 
>   def square(x):
>       return x*x
>   map(square, range(1000))
> 
> versus
> 
>   [x*x for x in range(1000)]
> 
> Hint: function calls are expensive.
> 

Some timings to verify this:

$ python -m timeit -s "def square(x): return x*x" "map(square, range(1000))"
1000 loops, best of 3: 693 usec per loop

$ python -m timeit -s "[x*x for x in range(1000)]"
10000000 loops, best of 3: 0.0505 usec per loop

Note that list comprehensions are also C-implemented, AFAIK.

Steve



More information about the Python-list mailing list