1

我一直在安装 linux mint (debian) 的旧笔记本电脑上使用 rstudio 服务器。

我一直在 Windows 上运行,所以我从来没有利用parallelmulticore包,我的目标是学习rstudio server以及R linux多核处理如何加速我的进程。

我每天使用的 lapply 的一个主要用途如下所示:

f <- function(x) {
   x1 <- data[1:50, x]
   x2 <- data[51:100, x]

   line <- c(paste0(mean(x1), " (", sd(x1), ")"),
             paste0(mean(x2), " (", sd(x2), ")"),
             t.test(x1, x2)$p.value)
   return(line)
 }

data <- data.frame(matrix(rnorm(2600, 85, 19), nrow=100, ncol=26))
names(data) <- letters

do.call(rbind, lapply(letters, f))

 microbenchmark(
     do.call(rbind, lapply(letters, f))
 )

中位时间为21.8 毫秒

或者:

library(parallel)
microbenchmark(
     do.call(rbind, mclapply(letters, f))
)

中位时间是120.9毫秒。

为什么会有这么大的差异?

该机器是2核恐龙。是不是在使用 >= 4 核机器之前您看不到好处?我的用例(data.frame 的按列计算)是否不适合看到好处?

谢谢!

4

1 回答 1

1

你的数据太小了,对开销有优势,试试

f <- function(x) {
  x1 <- data[1:50000, x]
  x2 <- data[50001:100000, x]

  line <- c(paste0(mean(x1), " (", sd(x1), ")"),
            paste0(mean(x2), " (", sd(x2), ")"),
            t.test(x1, x2)$p.value)
  return(line)
}

data <- data.frame(matrix(rnorm(2600, 85, 19), nrow=100000, ncol=26))

而是检查结果。您的示例使我的笔记本电脑的中位数为 7 毫秒和 17 毫秒,但我更大的示例将其更改为 120 和 80 毫秒。因此,在我看来,这(不仅是)核心数量,而且在这种情况下更多的是您的数据大小。

于 2015-09-09T06:14:37.830 回答