[英]why "in" generator is much faster than "in" list in python
Example:例子:
from timeit import timeit
print(timeit("5 in [i for i in range(0, 100)]"))
print(timeit("5 in map(int, range(0, 100))"))
and this is result:这是结果:
3.771566713000084
0.9066896029999043
python 3.8.5 (also I think this make no reference to python version ^_^) python 3.8.5(我也认为这没有提到python版本^_^)
The in
on map
(which is an iterator, not a generator, technically speaking; generators are functions using yield
or generator expressions, and they're a subset of the broader class of iterators) short-circuits as soon as it knows the result to be True
, so it only actually produces and checks six values and then immediately returns True
. map
上的in
(从技术上讲,它是迭代器,而不是生成器;生成器是使用yield
或生成器表达式的函数,它们是更广泛的迭代器类的子集)一旦知道结果就会短路为True
,因此它实际上只生成并检查六个值,然后立即返回True
。 The list
comprehension, by contrast, must produce the entire list
of 100 elements before checking any of them.该list
理解,相反,必须出示整个list
检查其中的任何前100个元素。
If your test was for an element that wasn't in the iterable in question, map
's win, if any (the pointless call to int
hurts it, performance-wise), would be smaller, but when the iterable contains the element, and it's early in the iterable, short-circuiting is clearly faster even if each element is more costly to produce, because it produces so many fewer elements.如果您的测试是针对不在所讨论的迭代中的元素,则map
的胜利,如果有的话(对int
的毫无意义的调用会损害它,性能方面)会更小,但是当迭代包含该元素时,并且在可迭代的早期,即使每个元素的生产成本更高,短路也明显更快,因为它产生的元素少得多。
Generator expressions are much better than list comprehensions.生成器表达式比列表推导式要好得多。
Iterating over the generator expression or the list comprehension will do the same thing.迭代生成器表达式或列表推导会做同样的事情。 However, the list comprehension will create the entire list in memory first while the generator expression will create the items on the fly, so you are able to use it for very large and also even infinite sequences但是,列表推导将首先在内存中创建整个列表,而生成器表达式将动态创建项目,因此您可以将其用于非常大甚至无限的序列
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.