Filter versus comprehension (was Re: something about split()???)

Terry Reedy tjreedy at udel.edu
Fri Aug 24 12:04:54 EDT 2012


On 8/24/2012 10:44 AM, Ramchandra Apte wrote:
> On Wednesday, 22 August 2012 22:13:04 UTC+5:30, Terry Reedy  wrote:

>>   >>> timeit.timeit("list(i for i in ranger if False)", "ranger=range(0)")
 >>
>> 0.91
>>
>>   >>> timeit.timeit("list(i for i in ranger if False)", "ranger=range(20)")
>>
>> 1.28
>>
>>   >>> timeit.timeit("list(filter(lambda i: False, ranger))",
>>
>> "ranger=range(0)")
>>
>> 0.83
>>
>>   >>> timeit.timeit("list(filter(lambda i: False, ranger))",
>>
>> "ranger=range(20)")
>>
>> 2.60

Your mail agent in inserting blank lines in quotes -- google?
See if you can turn that off.

> Your examples with lambda i:False are unrealistic - you are comparing
 > `if False` vs <lambda function>(xx) - function call vs boolean check

That is exactly the comparison I wanted to make. The iteration + boolean 
check takes .37 for 20 items, the iteration + call 1.77.

-- 
Terry Jan Reedy




More information about the Python-list mailing list