【问题标题】:why is python so much faster at editing column properties than r为什么 python 在编辑列属性时比 r 快得多
【发布时间】:2019-10-30 14:13:06
【问题描述】:

我一直在使用 spotfire,并意识到我的 python 代码编辑列属性的速度比 r 代码快得多。 r 代码大约需要 24 秒,而 python 代码大约需要 4 来完成相同的操作。我的 r 代码是不是写得不好才导致这种情况发生。

这是我的python代码示例:

start=time.time()
count=0
names=[]
for i in olddt.Columns: #getting columns from old data table
    names.append(i)

for i in dt.Columns: #assigning new values
    if count<=4:
        i.Properties["Limits.Whatif.Upper"]=1.0
        i.Properties["Limits.Whatif.Lower"]=1.0
        i.Properties["Limits.Prod.Upper"]=1.0
        i.Properties["Limits.Prod.Lower"]=1.0
        count=count+1
    else:
        i.Properties["Limits.Whatif.Upper"]=float(count-4)+26.0
        i.Properties["Limits.Whatif.Lower"]=float(count-4)-39.0
        i.Properties["Limits.Prod.Upper"]=names[count-4].Properties["Limits.Whatif.Upper"]+5.0
        i.Properties["Limits.Prod.Lower"]=names[count-4].Properties["Limits.Whatif.Lower"]-4.0
        count=count+1

print time.time()-start

这是我的 R 代码:

for(col in 1:ncol(temp2)){
    if (col<=4){
        attributes(temp2[,col])$SpotfireColumnMetaData$upper=Inf
        attributes(temp2[,col])$SpotfireColumnMetaData$lower=-1*Inf
        attributes(temp2[,col])$SpotfireColumnMetaData$upper2=Inf
        attributes(temp2[,col])$SpotfireColumnMetaData$lower2=-1*Inf
    }
    else{
        names(attributes(dt[,col-4])$SpotfireColumnMetaData)<- lapply( names( attributes(dt[ ,col-4] )$SpotfireColumnMetaData), tolower)
        attributes(temp2[,col])$SpotfireColumnMetaData$upper=2
        attributes(temp2[,col])$SpotfireColumnMetaDatalower=1
        attributes(temp2[,col])$SpotfireColumnMetaData$upper2=attributes(dt[,col-4])$SpotfireColumnMetaData$upper
        attributes(temp2[,col])$SpotfireColumnMetaData$lower2=attributes(dt[,col-4])$SpotfireColumnMetaData$lower
    }
}

我还使用了这里看到的 lapply 函数:

applyLimits <- function(col){
    if (count<4){
        attributes(temp2[,col])$SpotfireColumnMetaData$upper<<-Inf
        attributes(temp2[,col])$SpotfireColumnMetaData$lower<<- (-1*Inf)
        attributes(temp2[,col])$SpotfireColumnMetaData$upper2<<-Inf
        attributes(temp2[,col])$SpotfireColumnMetaData$lower2<<- (-1*Inf)
        count<<-count+1
    }
    else{
        attributes(temp2[,col])$SpotfireColumnMetaData$upper<<-2
        attributes(temp2[,col])$SpotfireColumnMetaData$lower<<-1
        attributes(temp2[,col])$SpotfireColumnMetaData$upper2<<-attributes(dt[,col-4])$SpotfireColumnMetaData$upper2
        attributes(temp2[,col])$SpotfireColumnMetaData$lower2<<-attributes(dt[,col-4])$SpotfireColumnMetaData$lower2
        count<<-count+1
    }
}

lapply(1:ncol(temp),applyLimits)

如果有什么方法可以改进我的 r 代码,请告诉我,但我还没有看到更好的方法来调整它的属性。根据我所做的一些研究 temp2 和 dt 都应该是 data.frame

【问题讨论】:

  • R 中的 For 循环是 notoriously inefficient。如果你想加快你的 R 代码,看看答案here。
  • 我认为使用data.table-package 可以获得很多性能。它可以通过引用更改(以及其他)属性,使用data.table::setattr().. 这可以节省内存并且应该大大加快速度..
  • 我对 R 不熟悉,但你应该看看它们与 python 等价物的时间复杂度差异。例如,尝试检查引擎盖下的工作方式。 python中的for循环,如果它针对一个列表的复杂度为O(n),并且该值的集合的复杂度为O(1),这使得它的复杂度为O(n)。似乎您在 R 中使用的循环具有更大的时间复杂度。
  • 添加一些temp2 的样本数据可以提高您获得好答案的机会。
  • @Wimpel 你有任何将 data.table 与来自 spotfire 的数据表一起使用的例子吗?不过我会调查一下。

标签: python r spotfire


【解决方案1】:

记住 R 是矢量化语言,您的 lapply 函数不是矢量化的。为了获得良好的性能,您需要 lapply 返回一个向量并一次性更新整个向量。您的函数一次更新一行和一列,这就是您性能不佳的原因。

矢量化方法是四个 lapply 调用,每个调用更新一整列。应该看起来像这样:

applyLimits1 <- function(col){
  count <<- count+1
  if (count<4) Inf else 2 
}
applyLimits2 <- function(col){
  count <<- count+1
  if (count<4) (-1*Inf) else 1 
}
applyLimits3 <- function(col){
  count <<- count+1
  if (count<4) Inf else attributes(dt[,col-4])$SpotfireColumnMetaData$upper2
}
applyLimits4 <- function(col){
  count <<- count+1
  if (count<4) (-1*Inf) else attributes(dt[,col-4])$SpotfireColumnMetaData$lower2
}

count <- -1
attributes(temp2[,col])$SpotfireColumnMetaData$upper <- lapply(1:ncol(temp),applyLimits1)
count <- -1
attributes(temp2[,col])$SpotfireColumnMetaData$lower <- lapply(1:ncol(temp),applyLimits2)
count <- -1
attributes(temp2[,col])$SpotfireColumnMetaData$upper2 <- lapply(1:ncol(temp),applyLimits3)
count <- -1
attributes(temp2[,col])$SpotfireColumnMetaData$lower2 <- lapply(1:ncol(temp),applyLimits4)

我没有要测试的数据,我刚刚粘贴了您的代码。使用 sapply 或 vapply 可能会更好。当然,有些语言比其他语言更适合某些任务......

【讨论】:

  • 我认为这段代码行不通。我也需要 col 变量增加。所以,attributes(temp2[,5])$SpotfireColumnMetaData$upper&lt;-attributes(dt[,1])$SpotfireColumnMetaData$upper2 然后attributes(temp2[,6])$SpotfireColumnMetaData$upper&lt;-attributes(dt[,2])$SpotfireColumnMetaData$upper2
猜你喜欢
  • 1970-01-01
  • 2015-01-30
  • 2013-06-03
  • 1970-01-01
  • 2016-03-21
  • 2014-04-21
  • 2015-02-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多