Python list comprehension vs generator

前端 未结 3 1411
后悔当初
后悔当初 2021-01-07 08:47

I found this question Generators vs List Comprehension performance in Python and instead of cProfile I use timeit.

from timeit import timeit
import cProfile
         


        
3条回答
  •  醉酒成梦
    2021-01-07 09:02

    Generators load lazily; you have to make a call to get their next value every time you want it.

    sum is an aggregate function, which operations on the entire iterable. You have to have all of the values available for it to do its work.

    The reason that the list comprehension works faster is that there's only one explicit call to get the entire list, and one explicit operation to sum them all. However, with the generator, you have to get all of the items for it to to perform its aggregation, and since there's a million of them, that results in a million calls.

    This is one of those cases in which being eager is better for performance.

提交回复
热议问题