为什么在for循环中,小动作比矩阵慢,但是在经常访问时,小动作却比矩阵快?

时间:2019-05-06 15:13:16

标签: r performance for-loop tidyverse tibble

对于基于代理的建模项目,我正在考虑使用tidyverse的tibble而不是matrix。我用一个非常简单的ABM(请参阅下文)检查了两者的性能,在该模型中我模拟了一个人口老化,死亡和出生的人口。对于ABM典型,我使用的是for循环和索引。

在对两个数据结构进行基准测试时(请参见此处的图形:https://github.com/marcosmolla/tibble_vs_matrix),矩阵比平移要快得多。但是,对于10e6运行,此结果实际上是相反的。而且我不知道为什么。

很高兴了解这个结果,以告知我将来是否应该针对这种用例使用小块或矩阵。

谢谢大家的投入!

fig1

# This code benchmarks the speed of tibbles versus matrices. This should be useful for evaluating the suitability of tibbles in a ABM context where matrix data is frequently altered in matrices (or vectors).

library(tidyverse)
library(reshape2)
library(cowplot)

lapply(c(10^1, 10^2, 10^3, 10^4, 10^5, 10^6), function(runtime){
  # Set up tibble
  indTBL <- tibble(id=1:100,
         type=sample(1:3, size=100, replace=T),
         age=1)

  # Set up matrix (from tibble)
  indMAT <- as.matrix(indTBL)

  # Simulation run with tibble
  t <- Sys.time()
  for(i in 1:runtime){
    # increase age
    indTBL$age <- indTBL[["age"]]+1

    # replace individuals by chance or when max age
    dead <- (1:100)[runif(n=100,min=0,max=1)<=0.01 | indTBL[["age"]]>100]
    indTBL[dead, "age"] <- 1
    indTBL[dead, "type"] <- sample(1:3, size=length(dead), replace=T)
  }
  tibbleTime <- as.numeric(Sys.time()-t)

  # Simulation run with matrix
  t <- Sys.time()
  for(i in 1:runtime){
    # increase age
    indMAT[,"age"] <- indMAT[,"age"]+1

    # replace individuals by chance or when max age
    dead <- (1:100)[runif(n=100,min=0,max=1)<=0.01 | indMAT[,"age"]>100]
    indMAT[dead, "age"] <- 1
    indMAT[dead, "type"] <- sample(1:3, size=length(dead), replace=T)
  }
  matrixTime <- as.numeric(Sys.time()-t)

  # Return both run times
  return(data.frame(tibbleTime=tibbleTime, matrixTime=matrixTime))
}) %>% bind_rows() -> res

# Prepare data for ggplot
res$power <- 1:nrow(res)
res_m <- melt(data=res, id.vars="power")

# Line plot for results
ggplot(data=res_m, aes(x=power, y=value, color=variable)) + geom_point() + geom_line() + scale_color_brewer(palette="Paired") + ylab("Runtime in sec") + xlab(bquote("Simulation runs"~10^x))

1 个答案:

答案 0 :(得分:0)

谢谢你们的答复。我使用microbenchmark软件包进行了正确的基准测试。现在,我发现对于10e6运行,矩阵仍然更快。

  indTBL <- tibble(id=1:100,
                   type=sample(1:3, size=100, replace=T),
                   age=1)

  # Set up matrix (from tibble)
  indMAT <- as.matrix(indTBL)

  # Simulation run with tibble
  runtime <- 10^6
  microbenchmark(
  tib=for(i in 1:runtime){
    # increase age
    indTBL$age <- indTBL[["age"]]+1

    # replace individuals by chance or when max age
    dead <- (1:100)[runif(n=100,min=0,max=1)<=0.01 | indTBL[["age"]]>100]
    indTBL[dead, "age"] <- 1
    indTBL[dead, "type"] <- sample(1:3, size=length(dead), replace=T)
  },

  # Simulation run with matrix
  mat=for(i in 1:runtime){
    # increase age
    indMAT[,"age"] <- indMAT[,"age"]+1

    # replace individuals by chance or when max age
    dead <- (1:100)[runif(n=100,min=0,max=1)<=0.01 | indMAT[,"age"]>100]
    indMAT[dead, "age"] <- 1
    indMAT[dead, "type"] <- sample(1:3, size=length(dead), replace=T)
  }, times=1
  )

结果是

Unit: seconds
 expr      min       lq     mean   median       uq      max neval cld
  tib 80.22042 81.45051 82.26645 82.68061 83.28946 83.89831     3   b
  mat 20.44746 20.66974 20.75168 20.89202 20.90378 20.91555     3  a 

感谢Ilrs和MrFlick的提示。