我一直在安装 linux mint (debian) 的旧笔记本电脑上使用 rstudio 服务器。
我一直在 Windows 上运行,所以我从来没有利用parallel
或multicore
包,我的目标是学习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 的按列计算)是否不适合看到好处?
谢谢!