为什么元组在评估生成器时比列表慢?(Why are tuples slower than lists at evaluating generators into themselves?)

编程入门 行业动态 更新时间:2024-10-21 23:20:19
为什么元组在评估生成器时比列表慢?(Why are tuples slower than lists at evaluating generators into themselves?) python -m timeit "tuple(xrange(600000))"

100个循环,最佳3:每循环11.5毫秒

python -m timeit "list(xrange(600000))"

100个循环,最佳3:每循环10.1毫秒


将它们与dis模块进行比较:

>>> from dis import dis >>> dis(lambda: tuple(xrange(600000))) 0 LOAD_GLOBAL 0 (tuple) 3 LOAD_GLOBAL 1 (xrange) 6 LOAD_CONST 1 (600000) 9 CALL_FUNCTION 1 12 CALL_FUNCTION 1 15 RETURN_VALUE >>> dis(lambda: list(xrange(600000))) 0 LOAD_GLOBAL 0 (list) 3 LOAD_GLOBAL 1 (xrange) 6 LOAD_CONST 1 (600000) 9 CALL_FUNCTION 1 12 CALL_FUNCTION 1 15 RETURN_VALUE python -m timeit "tuple(xrange(600000))"

100 loops, best of 3: 11.5 msec per loop

python -m timeit "list(xrange(600000))"

100 loops, best of 3: 10.1 msec per loop


Comparing them with the dis module:

>>> from dis import dis >>> dis(lambda: tuple(xrange(600000))) 0 LOAD_GLOBAL 0 (tuple) 3 LOAD_GLOBAL 1 (xrange) 6 LOAD_CONST 1 (600000) 9 CALL_FUNCTION 1 12 CALL_FUNCTION 1 15 RETURN_VALUE >>> dis(lambda: list(xrange(600000))) 0 LOAD_GLOBAL 0 (list) 3 LOAD_GLOBAL 1 (xrange) 6 LOAD_CONST 1 (600000) 9 CALL_FUNCTION 1 12 CALL_FUNCTION 1 15 RETURN_VALUE

最满意答案

由于迭代器通常不会为您提供前期大小,因此元组和列表都需要使用过度分配策略来处理任意大小的迭代。 就目前而言, xrange()对象确实有一个__len__方法,并且元组和列表使用的_PyObject_LengthHint()函数将利用它来设置一次正确的目标大小。 所以在这种情况下, list()代码只是巧妙地提高效率,因为它内联迭代以避免NULL比较。

跟我来完成代码; 在这种情况下唯一真正的区别是如何解开迭代器并复制值。 因为list()和tuple()对象跟踪不同的信息,所以这些循环在实现中略有不同。 看到:

PySequence_Tuple()用于tuple() 用于list() listextend() list()

tuple()代码路径使用PyIter_Next()而list()代码路径内联则不必测试NULL两次。 除此之外,循环执行相同的工作量。 我认为这是NULL测试,放大超过60万次迭代,这就解释了这里的时差。

无论如何,你发现的时差确实不大; 在我的机器上重复运行将时间差保持在10%以内(每次list获胜)。 时间差随着使用的xrange()的大小线性增加。

As iterators normally don't give you an up-front size, both tuples and lists need to use an overallocation strategy to handle arbitrary size iterables. As it stands, xrange() objects do have a __len__ method, and the _PyObject_LengthHint() function used used by both tuples and lists will take advantage of this to set a correct target size, once. So in this case, the list() code is just subtly more efficient because it inlines iteration to avoid a NULL comparison.

Follow me through the code; the only real difference in this case is how the iterator is unwound and the values are copied across. Because list() and tuple() objects track different information, these loops are subtly different in implementation. See:

PySequence_Tuple() is used for tuple() listextend() used for list()

The tuple() code path uses PyIter_Next() while the list() code path inlines that to not have to test for NULL twice. Apart from that the loops do the same amount of work. I think it is that NULL test, amplified over 600000 iterations, that account for the time difference here.

In any case, the time difference you found is really not that big; repeated runs on my machine keep the difference in time within 10% (with list winning each time). The time difference increases linearly with the size of the xrange() used.

更多推荐

本文发布于:2023-08-03 13:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1391993.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:生成器   列表   tuples   slower   evaluating

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!