R的并行运算

rm(list=ls())
time1 <- Sys.time()
library(parallel)
#打开四核,具体核数根据机器的核数决定
cl <- makeCluster(getOption('cl.cores', 1))
#---------------
sumtest <- function(x){
result = x + 1
}
per <- parSapply(cl,c(0:100000),sumtest)

time2 <- Sys.time()
(time.run <- time2 - time1)

  

注意: 根据计算类型定,不一定开的核数越多运行速度越快,简单的一般是转化为sapply或者是lapply的形式

原文地址:https://www.cnblogs.com/lmj-sky/p/9317013.html