简体   繁体   English

为什么“in”生成器比python中的“in”列表快得多

[英]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.

相关问题 为什么numpy数组看起来不比标准的python列表快得多? - Why does a numpy array not appear to be much faster than a standard python list? 为什么列表理解比附加到列表快得多? - Why is a list comprehension so much faster than appending to a list? Python:为什么迭代一个列表比迭代其长度上的xrange()生成器定义要快? - Python: why is it faster to iterate over a list than iterating over an xrange() generator define on its length? 为什么列表理解比乘法数组的numpy要快得多? - Why list comprehension is much faster than numpy for multiplying arrays? 为什么复制列表的方法比其他方法快得多? - Why is this method of copying a list so much faster than these others? 为什么这个列表理解比等效的生成器表达更快? - Why this list comprehension is faster than equivalent generator expression? 为什么求和列表理解比生成器表达式更快? - Why is summing list comprehension faster than generator expression? 为什么将列表转换为集合要比将生成器转换为集合快? - Why converting list to set is faster than converting generator to set? 为什么使用Python生成器来遍历二叉树要慢得多? - Why is using a Python generator much slower to traverse binary tree than not? Python清单:为什么.sort()比sorted()快得多? - Python lists: why is .sort() much faster than sorted()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM