本文介绍了lambda与列表理解性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近发布了一个使用lambda函数的问题,有人在答复中提到lambda不再受欢迎,而是使用列表推导.我对Python比较陌生.我进行了一个简单的测试:

I recently posted a question using a lambda function and in a reply someone had mentioned lambda is going out of favor, to use list comprehensions instead. I am relatively new to Python. I ran a simple test:

import time

S=[x for x in range(1000000)]
T=[y**2 for y in range(300)]
#
#
time1 = time.time()
N=[x for x in S for y in T if x==y]
time2 = time.time()
print 'time diff [x for x in S for y in T if x==y]=', time2-time1
#print N
#
#
time1 = time.time()
N=filter(lambda x:x in S,T)
time2 = time.time()
print 'time diff filter(lambda x:x in S,T)=', time2-time1
#print N
#
#
#http://snipt.net/voyeg3r/python-intersect-lists/
time1 = time.time()
N = [val for val in S if val in T]
time2 = time.time()
print 'time diff [val for val in S if val in T]=', time2-time1
#print N
#
#
time1 = time.time()
N= list(set(S) & set(T))
time2 = time.time()
print 'time diff list(set(S) & set(T))=', time2-time1
#print N  #the results will be unordered as compared to the other ways!!!
#
#
time1 = time.time()
N=[]
for x in S:
    for y in T:
        if x==y:
            N.append(x)
time2 = time.time()
print 'time diff using traditional for loop', time2-time1
#print N

它们都打印相同的N,所以我评论说打印出stmt(最后一种方法是无序的),但是在重复的测试中,由此产生的时差很有趣,如以下示例所示:

They all print the same N so I commented that print stmt out (except the last way it's unordered), but the resulting time differences were interesting over repeated tests as seen in this one example:

time diff [x for x in S for y in T if x==y]= 54.875
time diff filter(lambda x:x in S,T)= 0.391000032425
time diff [val for val in S if val in T]= 12.6089999676
time diff list(set(S) & set(T))= 0.125
time diff using traditional for loop 54.7970001698

因此,尽管我觉得整体列表理解更容易阅读,但至少在此示例中似乎存在一些性能问题.

So while I find list comprehensions on the whole easier to read, there seems to be some performance issues at least in this example.

因此,有两个问题:

  1. 为什么将lambda等推开?

  1. Why is lambda etc being pushed aside?

对于列表理解方式,是否有更有效的实现方式?您如何知道不进行测试就更有效?我的意思是,由于额外的函数调用,lambda/map/filter的效率较低,但似乎效率更高.

For the list comprehension ways, is there a more efficient implementation and how would you KNOW it's more efficient without testing? I mean, lambda/map/filter was supposed to be less efficient because of the extra function calls, but it seems to be MORE efficient.

保罗

推荐答案

您的测试所做的事情非常不同. S为1M元素,T为300:

Your tests are doing very different things. With S being 1M elements and T being 300:

[x for x in S for y in T if x==y]= 54.875

此选项进行300M相等性比较.

This option does 300M equality comparisons.

filter(lambda x:x in S,T)= 0.391000032425

此选项可对S执行300次线性搜索.

This option does 300 linear searches through S.

[val for val in S if val in T]= 12.6089999676

此选项通过T进行1M线性搜索.

This option does 1M linear searches through T.

list(set(S) & set(T))= 0.125

此选项可进行两种设置构造和一种设置交点.

This option does two set constructions and one set intersection.

这些选项之间的性能差异与每个选项所使用的算法更为相关,而不是列表理解和lambda之间的任何差异.

The differences in performance between these options is much more related to the algorithms each one is using, rather than any difference between list comprehensions and lambda.

这篇关于lambda与列表理解性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 09:44