Python 列表理解与生成器表达式';结果如何?
我在回答这个问题时,我更喜欢这里的生成器表达式,并使用了这个表达式,我认为它会更快,因为生成器不需要首先创建整个列表:Python 列表理解与生成器表达式';结果如何?,python,list,list-comprehension,timeit,generator-expression,Python,List,List Comprehension,Timeit,Generator Expression,我在回答这个问题时,我更喜欢这里的生成器表达式,并使用了这个表达式,我认为它会更快,因为生成器不需要首先创建整个列表: >>> lis=[['a','b','c'],['d','e','f']] >>> 'd' in (y for x in lis for y in x) True Levon在他的书中使用了列表理解 但当我计算时间时,这些LC的结果比发电机快: ~$ python -m timeit -s "lis=[['a','b','c'],['d',
>>> lis=[['a','b','c'],['d','e','f']]
>>> 'd' in (y for x in lis for y in x)
True
Levon在他的书中使用了列表理解
但当我计算时间时,这些LC的结果比发电机快:
~$ python -m timeit -s "lis=[['a','b','c'],['d','e','f']]" "'d' in (y for x in lis for y in x)"
100000 loops, best of 3: 2.36 usec per loop
~$ python -m timeit -s "lis=[['a','b','c'],['d','e','f']]" "'d' in [y for x in lis for y in x]"
100000 loops, best of 3: 1.51 usec per loop
然后我增加了列表的大小,并再次计时:
lis=[['a','b','c'],['d','e','f'],[1,2,3],[4,5,6],[7,8,9],[10,11,12],[13,14,15],[16,17,18]]
这次搜索'd'
生成器的速度比LC快,但当我搜索中间元素(11)和最后一个元素时,LC再次击败生成器表达式,我不明白为什么
~$ python -m timeit -s "lis=[['a','b','c'],['d','e','f'],[1,2,3],[4,5,6],[7,8,9],[10,11,12],[13,14,15],[16,17,18]]" "'d' in (y for x in lis for y in x)"
100000 loops, best of 3: 2.96 usec per loop
~$ python -m timeit -s "lis=[['a','b','c'],['d','e','f'],[1,2,3],[4,5,6],[7,8,9],[10,11,12],[13,14,15],[16,17,18]]" "'d' in [y for x in lis for y in x]"
100000 loops, best of 3: 7.4 usec per loop
~$ python -m timeit -s "lis=[['a','b','c'],['d','e','f'],[1,2,3],[4,5,6],[7,8,9],[10,11,12],[13,14,15],[16,17,18]]" "11 in [y for x in lis for y in x]"
100000 loops, best of 3: 5.61 usec per loop
~$ python -m timeit -s "lis=[['a','b','c'],['d','e','f'],[1,2,3],[4,5,6],[7,8,9],[10,11,12],[13,14,15],[16,17,18]]" "11 in (y for x in lis for y in x)"
100000 loops, best of 3: 9.76 usec per loop
~$ python -m timeit -s "lis=[['a','b','c'],['d','e','f'],[1,2,3],[4,5,6],[7,8,9],[10,11,12],[13,14,15],[16,17,18]]" "18 in (y for x in lis for y in x)"
100000 loops, best of 3: 8.94 usec per loop
~$ python -m timeit -s "lis=[['a','b','c'],['d','e','f'],[1,2,3],[4,5,6],[7,8,9],[10,11,12],[13,14,15],[16,17,18]]" "18 in [y for x in lis for y in x]"
100000 loops, best of 3: 7.13 usec per loop
与流行的观点相反,列表理解对于中等范围来说是非常好的。迭代器协议意味着调用
迭代器。uuu next_uuu()
,而Python中的函数调用——老实说——代价非常昂贵
当然,在某个时候,生成器内存/cpu的权衡将开始奏效,但对于小集合,列表理解是非常有效的。完全取决于数据 发电机有一个固定的设置时间,必须在调用多少项上摊销;列表理解最初更快,但随着较大数据集使用更多内存,其速度会大大降低 回想一下,随着cPython列表的扩展,列表的大小将按照的增长模式进行调整。对于更大的列表理解,Python可能会分配比数据大小多4倍的内存。一旦你击中虚拟机——真的很慢!但是,如上所述,对于小数据集,列表理解比生成器更快 考虑案例1,一个2x26列表:
LoL=[[c1,c2] for c1,c2 in zip(string.ascii_lowercase,string.ascii_uppercase)]
def lc_d(item='d'):
return item in [i for sub in LoL for i in sub]
def ge_d(item='d'):
return item in (y for x in LoL for y in x)
def any_lc_d(item='d'):
return any(item in x for x in LoL)
def any_gc_d(item='d'):
return any([item in x for x in LoL])
def lc_z(item='z'):
return item in [i for sub in LoL for i in sub]
def ge_z(item='z'):
return item in (y for x in LoL for y in x)
def any_lc_z(item='z'):
return any(item in x for x in LoL)
def any_gc_z(item='z'):
return any([item in x for x in LoL])
cmpthese.cmpthese([lc_d,ge_d,any_gc_d,any_gc_z,any_lc_d,any_lc_z, lc_z, ge_z])
这些时间安排的结果:
rate/sec ge_z lc_z lc_d any_lc_z any_gc_z any_gc_d ge_d any_lc_d
ge_z 124,652 -- -10.1% -16.6% -44.3% -46.5% -48.5% -76.9% -80.7%
lc_z 138,678 11.3% -- -7.2% -38.0% -40.4% -42.7% -74.3% -78.6%
lc_d 149,407 19.9% 7.7% -- -33.3% -35.8% -38.2% -72.3% -76.9%
any_lc_z 223,845 79.6% 61.4% 49.8% -- -3.9% -7.5% -58.5% -65.4%
any_gc_z 232,847 86.8% 67.9% 55.8% 4.0% -- -3.7% -56.9% -64.0%
any_gc_d 241,890 94.1% 74.4% 61.9% 8.1% 3.9% -- -55.2% -62.6%
ge_d 539,654 332.9% 289.1% 261.2% 141.1% 131.8% 123.1% -- -16.6%
any_lc_d 647,089 419.1% 366.6% 333.1% 189.1% 177.9% 167.5% 19.9% --
现在考虑<强>案例2 <强>,这显示了LC和GEN之间的巨大差异。在这种情况下,我们在100×97×97列表中寻找一种元素:列表结构:
LoL=[[str(a),str(b),str(c)]
for a in range(100) for b in range(97) for c in range(97)]
def lc_10(item='10'):
return item in [i for sub in LoL for i in sub]
def ge_10(item='10'):
return item in (y for x in LoL for y in x)
def any_lc_10(item='10'):
return any([item in x for x in LoL])
def any_gc_10(item='10'):
return any(item in x for x in LoL)
def lc_99(item='99'):
return item in [i for sub in LoL for i in sub]
def ge_99(item='99'):
return item in (y for x in LoL for y in x)
def any_lc_99(item='99'):
return any(item in x for x in LoL)
def any_gc_99(item='99'):
return any([item in x for x in LoL])
cmpthese.cmpthese([lc_10,ge_10,any_lc_10,any_gc_10,lc_99,ge_99,any_lc_99,any_gc_99],c=10,micro=True)
这些时间的结果:
rate/sec usec/pass ge_99 lc_99 lc_10 any_lc_99 any_gc_99 any_lc_10 ge_10 any_gc_10
ge_99 3 354545.903 -- -20.6% -30.6% -60.8% -61.7% -63.5% -100.0% -100.0%
lc_99 4 281678.295 25.9% -- -12.6% -50.6% -51.8% -54.1% -100.0% -100.0%
lc_10 4 246073.484 44.1% 14.5% -- -43.5% -44.8% -47.4% -100.0% -100.0%
any_lc_99 7 139067.292 154.9% 102.5% 76.9% -- -2.4% -7.0% -100.0% -100.0%
any_gc_99 7 135748.100 161.2% 107.5% 81.3% 2.4% -- -4.7% -100.0% -100.0%
any_lc_10 8 129331.803 174.1% 117.8% 90.3% 7.5% 5.0% -- -100.0% -100.0%
ge_10 175,494 5.698 6221964.0% 4943182.0% 4318339.3% 2440446.0% 2382196.2% 2269594.1% -- -38.5%
any_gc_10 285,327 3.505 10116044.9% 8036936.7% 7021036.1% 3967862.6% 3873157.1% 3690083.0% 62.6% --
正如您所看到的——这取决于函数调用的开销,这是一种折衷…扩展到的答案,生成器表达式通常比列表理解慢。在这种情况下,中的的短路行为会抵消在相当早的时间发现项目时的慢度,但在其他情况下,该模式保持不变
为了进行更详细的分析,我在分析器中运行了一个简单的脚本。以下是脚本:
lis=[['a','b','c'],['d','e','f'],[1,2,3],[4,5,6],
[7,8,9],[10,11,12],[13,14,15],[16,17,18]]
def ge_d():
return 'd' in (y for x in lis for y in x)
def lc_d():
return 'd' in [y for x in lis for y in x]
def ge_11():
return 11 in (y for x in lis for y in x)
def lc_11():
return 11 in [y for x in lis for y in x]
def ge_18():
return 18 in (y for x in lis for y in x)
def lc_18():
return 18 in [y for x in lis for y in x]
for i in xrange(100000):
ge_d()
lc_d()
ge_11()
lc_11()
ge_18()
lc_18()
以下是相关结果,重新排序以使模式更清晰
5400002 function calls in 2.830 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
100000 0.158 0.000 0.251 0.000 fop.py:3(ge_d)
500000 0.092 0.000 0.092 0.000 fop.py:4(<genexpr>)
100000 0.285 0.000 0.285 0.000 fop.py:5(lc_d)
100000 0.356 0.000 0.634 0.000 fop.py:8(ge_11)
1800000 0.278 0.000 0.278 0.000 fop.py:9(<genexpr>)
100000 0.333 0.000 0.333 0.000 fop.py:10(lc_11)
100000 0.435 0.000 0.806 0.000 fop.py:13(ge_18)
2500000 0.371 0.000 0.371 0.000 fop.py:14(<genexpr>)
100000 0.344 0.000 0.344 0.000 fop.py:15(lc_18)
我不确定剩余的时间是什么原因造成的;即使没有额外的函数调用,生成器表达式似乎也会慢一些。我想这证实了“创建生成器理解比列表理解有更多的本地开销”的断言。但无论如何,这非常清楚地表明,生成器表达式的速度较慢主要是因为调用了next
我要补充一点,当短路不起作用时,列表理解速度更快,即使对于非常大的列表也是如此。例如:
>>> counter = itertools.count()
>>> lol = [[counter.next(), counter.next(), counter.next()]
for _ in range(1000000)]
>>> 2999999 in (i for sublist in lol for i in sublist)
True
>>> 3000000 in (i for sublist in lol for i in sublist)
False
>>> %timeit 2999999 in [i for sublist in lol for i in sublist]
1 loops, best of 3: 312 ms per loop
>>> %timeit 2999999 in (i for sublist in lol for i in sublist)
1 loops, best of 3: 351 ms per loop
>>> %timeit any([2999999 in sublist for sublist in lol])
10 loops, best of 3: 161 ms per loop
>>> %timeit any(2999999 in sublist for sublist in lol)
10 loops, best of 3: 163 ms per loop
>>> %timeit for i in [2999999 in sublist for sublist in lol]: pass
1 loops, best of 3: 171 ms per loop
>>> %timeit for i in (2999999 in sublist for sublist in lol): pass
1 loops, best of 3: 183 ms per loop
正如您所看到的,当短路不相关时,即使对于百万项长的列表,列表理解也始终更快。显然,在这些标度下,实际使用中的,发电机会因为短路而更快。但是对于其他类型的迭代任务,在项目数量上是真正线性的,列表理解总是快得多。如果您需要在一个列表上执行多个测试,这一点尤其正确;您可以非常快速地迭代已构建的列表:
在这种情况下,列表理解速度要快一个数量级
当然,这只会在内存耗尽之前保持不变。这就引出了我的最后一点。使用发电机有两个主要原因:利用短路和节省内存。对于非常大的sequences/iterables,生成器是显而易见的方法,因为它们可以节省内存。但如果短路不是一个选项,你几乎永远不会选择发电机超过列表的速度。您选择它们是为了节省内存,这始终是一种折衷。+1我也会关注答案:)可能是因为缓存。。。和发电机。。。可能需要更多的调用(下一步、屈服、保存状态等)。而且生成器的内存效率也很高。这是肯定的。为什么不干脆any(x中的d代表lis中的x)
?@gnibblerany()
比生成器表达式本身慢,请看你上面提到的问题。生成器也不会耗尽整个列表,但它很慢。@Ashwini Chaudhary:与具有少量数据的LC相比,生成器只慢。查看案例2中的比较速度,发电机或使用发电机的any
结构踢LC的屁股。生成器速度很快,但它们必须克服较长的安装时间。@senderle:非常好的观点,我在文章的编辑中提到了它们。谢谢,这对我现在来说更有意义。此外,创建生成器理解比列表理解有更多的本机开销
>>> .634 - .278 - .333
0.023
>>> .806 - .371 - .344
0.091
>>> counter = itertools.count()
>>> lol = [[counter.next(), counter.next(), counter.next()]
for _ in range(1000000)]
>>> 2999999 in (i for sublist in lol for i in sublist)
True
>>> 3000000 in (i for sublist in lol for i in sublist)
False
>>> %timeit 2999999 in [i for sublist in lol for i in sublist]
1 loops, best of 3: 312 ms per loop
>>> %timeit 2999999 in (i for sublist in lol for i in sublist)
1 loops, best of 3: 351 ms per loop
>>> %timeit any([2999999 in sublist for sublist in lol])
10 loops, best of 3: 161 ms per loop
>>> %timeit any(2999999 in sublist for sublist in lol)
10 loops, best of 3: 163 ms per loop
>>> %timeit for i in [2999999 in sublist for sublist in lol]: pass
1 loops, best of 3: 171 ms per loop
>>> %timeit for i in (2999999 in sublist for sublist in lol): pass
1 loops, best of 3: 183 ms per loop
>>> incache = [2999999 in sublist for sublist in lol]
>>> get_list = lambda: incache
>>> get_gen = lambda: (2999999 in sublist for sublist in lol)
>>> %timeit for i in get_list(): pass
100 loops, best of 3: 18.6 ms per loop
>>> %timeit for i in get_gen(): pass
1 loops, best of 3: 187 ms per loop