Why this list comprehension is faster than equivalent generator expression?

前端 未结 2 417
南旧
南旧 2021-01-13 00:23

I\'m using Python 3.3.1 64-bit on Windows and this code snippet:

len ([None for n in range (1, 1000000) if n%3 == 1])

executes in 136ms, c

2条回答
  •  天命终不由人
    2021-01-13 00:58

    I believe the difference here is entirely in the cost of 1000000 additions. Testing with 64-bit Python.org 3.3.0 on Mac OS X:

    In [698]: %timeit len ([None for n in range (1, 1000000) if n%3 == 1])
    10 loops, best of 3: 127 ms per loop
    In [699]: %timeit sum (1 for n in range (1, 1000000) if n%3 == 1)
    10 loops, best of 3: 138 ms per loop
    In [700]: %timeit sum ([1 for n in range (1, 1000000) if n%3 == 1])
    10 loops, best of 3: 139 ms per loop
    

    So, it's not that the comprehension is faster than the genexp; they both take about the same time. But calling len on a list is instant, while summing 1M numbers adds another 7% to the total time.

    Throwing a few different numbers at it, this seems to hold up unless the list is very tiny (in which case it does seem to get faster), or large enough that memory allocation starts to become a significant factor (which it isn't yet, at 333K).

提交回复
热议问题