Search code examples
pythonpython-3.xperformanceany

The speed difference of two generators in any (built in function)


In this code, why is the second output faster than the first!?

Both do almost the same thing and have the same result.


from timeit import timeit

lst = [0] * 10000000

txt1 = "any(i % 2 for i in lst)"
txt2 = "any(True for i in lst if i % 2)"

print(timeit(txt1, globals=globals(), number=2))
print(timeit(txt2, globals=globals(), number=2))

result:

Time : 2.112963530991692
Time : 0.9412867689970881

Solution

  • The reason is due to what each generator produces. The first generator produces 10000000 False values. The second generator produces nothing, because i % 2 will never be non-zero.

    You can see a similar difference by removing the generator and the list creation from the timings:

    >>> x = []
    >>> y = [0] * 10000000
    >>> timeit("any(x)", globals=globals(), number=2)
    3.900029696524143e-06
    >>> timeit("any(y)", globals=globals(), number=2)
    0.12017650000052527