我最近遇到过这种行为,我有点困惑它为什么会发生——我最初的假设是,在调用函数时,而不是在运行语句时,正在进行某种优化
例如: 让我们从一个简单的例子开始:
somestring="climate change is a big problem. However emissions are still rising"
sometopics=["climate","change","problem","big","rising"]
假设我们有一个字符串列表,类似于上面的“somestring”,我们也有一个主题列表,比如somestopics
我们想比较“somestring”中是否存在任何“sometopics”,重要的是,将这些“sometopics”返回到一个新列表
使用列表理解语句,我们可以对一个字符串执行以下操作:
result = [element for element in sometopic if(element in somestring)]
然而,在我的机器上,下面的函数定义比上面的语句快20-30%
def comparelistoftopicstokw(mystring,somelistoftopics):
result = [element for element in somelistoftopics if(element in mystring)]
return result
为什么会发生这种情况
函数总是比等价语句/语句列表更快吗
编辑****
请参见以下最低可行可复制笔记本示例:
import pandas as pd, numpy as np
columns_df = pd.DataFrame({"Keyword":['fish soup','katsu','soup']}) # Compute a Pandas dataframe to write into 500kcolumns
somestring="pad thai is a good recipe. It is cooked with chicken or lamb or beef"
sometopics=["chicken","pad thai","recipe","lamb","beef"]
print(len(sometopics))
somebigtopics=sometopics*100000
def extractsubstrings(inputstring,alistofpossibletopics):
#obvious very slow for loop
topicslist=[]
print(inputstring)
for topic in alistofpossibletopics:
if str(topic) in inputstring:
topicslist.append(str(topic))
%%time
def listcompinlists(mystring,bigtopic):
res = [ele for ele in bigtopic if(ele in mystring)]
return res
%%time
res = [ele for ele in somebigtopics if(ele in somestring)]
%%time
x=extractsubstrings(somestring,somebigtopics)
%%time
funcres=listcompinlists(somestring,somebigtopics)
在我的机器上(Ubuntu18.04,Python3.6),对于上述情况,列表理解在22-24毫秒内执行,而函数在18-21毫秒内执行。这并不是很大的区别,但如果你有1000万行要处理,例如,这就节省了相当多的时间
TLDR Performance comparison:
extractsubstrings=Wall time: 122 ms
list comprehension statement: Wall time: 24.5 ms
listcompinlists=Wall time: 18.6 ms
我无法回答你的问题,但我做了一个小测试,对它的基础提出了质疑
正如我们可以从输出中推断的那样,结果是非常随机的,在某些情况下,一个平均比另一个快,而另一个则相反
输出:
我无法复制你所说的。你能提供证明你主张的任何测量数据吗
我创建此度量是为了比较执行时间:
输出
所以在我的例子中,列表的理解速度平均更快
相关问题 更多 >
编程相关推荐