【发布时间】:2015-06-01 17:19:24
【问题描述】:
我尝试使用并行包在 R 中测试并行。但在我的示例中(如下面的代码),并行任务的时间比单个任务的时间长。有人可以给我一些建议吗?
非常感谢!
##parSquareNum.R
strt <- Sys.time()
workerFunc <- function(n) { return(n^2) }
values <- 1:1000000
library(parallel)
## Number of workers (R processes) to use:
cores <- detectCores()
## Set up the ’cluster’
cl <- makeCluster(cores-1)
## Parallel calculation (parLapply):
res <- parLapply(cl, values, workerFunc)
## Shut down cluster
write(Sys.time()-strt, 'parallel.txt')
stopCluster(cl)
##singleSquareNum.R
## The worker function to do the calculation:
strt <- Sys.time()
workerFunc <- function(n) { return(n^2) }
## The values to apply the calculation to:
values <- 1:1000000
## Serial calculation:
res <- lapply(values, workerFunc)
##print(unlist(res))
write(Sys.time() -strt, 'single.txt')
【问题讨论】:
-
并行性不太可能对核心的简单任务有益,因为调度和重组过程也需要时间。需要将复杂的任务分拆出来以展示收益。
标签: r parallel-processing