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

Time:07-18

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

CodePudding user response:

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

CodePudding user response:

like @sj95126 said in the comments: the first produces 10000000 values for any() to check, the second generator produces 0 values

you can check it by calling list on it instead of any, in a more smaller sample of course

>>> lst = [0]*10
>>> any(i % 2 for i in lst)
False
>>> list(i % 2 for i in lst)
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
>>> any(True for i in lst if i % 2)
False
>>> list(True for i in lst if i % 2)
[]
>>> 
  • Related