为什么列表理解比等效的生成器表达式要快? [英] Why this list comprehension is faster than equivalent generator expression?

查看:109
本文介绍了为什么列表理解比等效的生成器表达式要快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows上使用64位Python 3.3.1,并且此代码段:

I'm using Python 3.3.1 64-bit on Windows and this code snippet:

len ([None for n in range (1, 1000000) if n%3 == 1])

在136ms内执行,与此相比:

executes in 136ms, compared to this one:

sum (1 for n in range (1, 1000000) if n%3 == 1)

在146毫秒内执行.在这种情况下,生成器表达式不应该更快或与列表理解速度相同吗?

which executes in 146ms. Shouldn't a generator expression be faster or the same speed as the list comprehension in this case?

我引用了Guido van Rossum的话来自列表对生成器表达式的理解:

I quote from Guido van Rossum From List Comprehensions to Generator Expressions:

... Python 3中的列表推导和生成器表达式都是 实际上比Python 2快! (并且不再有 两者之间的速度差异.)

...both list comprehensions and generator expressions in Python 3 are actually faster than they were in Python 2! (And there is no longer a speed difference between the two.)

我用timeit测量了时间.我知道它不是很准确,但是我只关心相对速度,在使用不同数量的迭代进行测试时,列表理解版本的时间越来越短.

I measured the time with timeit. I know that it is not very accurate, but I care only about relative speeds here and I'm getting consistently shorter time for list comprehension version, when I test with different numbers of iterations.

推荐答案

我相信这里的区别完全在于增加1000000个附加项的成本.在Mac OS X上使用64位Python.org 3.3.0进行测试:

I believe the difference here is entirely in the cost of 1000000 additions. Testing with 64-bit Python.org 3.3.0 on Mac OS X:

In [698]: %timeit len ([None for n in range (1, 1000000) if n%3 == 1])
10 loops, best of 3: 127 ms per loop
In [699]: %timeit sum (1 for n in range (1, 1000000) if n%3 == 1)
10 loops, best of 3: 138 ms per loop
In [700]: %timeit sum ([1 for n in range (1, 1000000) if n%3 == 1])
10 loops, best of 3: 139 ms per loop

因此,并不是说理解速度要比genexp快.他们都花费大约相同的时间.但是,在list上调用len是即时的,将100万个数字相加会增加总时间的7%.

So, it's not that the comprehension is faster than the genexp; they both take about the same time. But calling len on a list is instant, while summing 1M numbers adds another 7% to the total time.

给它加上几个不同的数字,除非列表非常小(在这种情况下, 似乎变得更快),或者足够大以至于内存分配开始变大,否则这似乎会持续下去.一个重要因素(目前还没有,只有333K).

Throwing a few different numbers at it, this seems to hold up unless the list is very tiny (in which case it does seem to get faster), or large enough that memory allocation starts to become a significant factor (which it isn't yet, at 333K).

这篇关于为什么列表理解比等效的生成器表达式要快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆