mclapply vs parLapply速度

时间:2016-08-15 15:09:51

标签: r parallel-processing mclapply

我在Linux上运行并轻松使用mclapply。即使在使用parlapply后,我也会遇到clusterEvalQ的一些错误。

在我进一步解决问题之前,是否有任何意义,即两者之间是否存在显着的速度差异,或者人们在Windows上使用parLapply时会不会这样做?

我已经阅读了parLapplyLB并且可以看到这种方法的用途,但如果我严格关注mclapplyparlapply那么FORK方法和PSOCK方法会有很大不同在速度?

我的功能的性质可能决定了答案;它正在使用stri_extract

1 个答案:

答案 0 :(得分:5)

一些快速基准测试表明mclapply可能稍快一些,但这可能取决于具体的系统和问题。作业越平衡,实际任务越慢,它就越重要,你使用哪种功能。

library(parallel)
library(microbenchmark)

microbenchmark(
  parLapply = {cl <- makeCluster(2)
  parLapply(cl, rep(1:7, 3), function(x) {set.seed(1); rnorm(10^x)})
  stopCluster(cl)},
  mclapply = {mclapply(rep(1:7 , 3), function(x) {set.seed(1); rnorm(10^x)}, mc.cores = 2)},
  times = 10
)

#Unit: seconds
#     expr     min      lq     mean   median       uq      max neval
#parLapply 1.85548 2.04397 3.332970 3.071284 4.323514 6.294364    10
#mclapply  1.62610 1.65288 2.217407 1.849594 2.243418 5.435189    10


microbenchmark(
  parLapply = {cl <- makeCluster(2)
  parLapply(cl, rep(6, 20), function(x) {set.seed(1); rnorm(10^x)})
  stopCluster(cl)},
  mclapply = {mclapply(rep(6, 20), function(x) {set.seed(1); rnorm(10^x)}, mc.cores = 2)},
  times = 10
)

#Unit: milliseconds
#     expr      min        lq      mean   median       uq      max neval
#parLapply 1150.657 1188.9750 1705.1364 1242.739 2071.276 3785.516    10
# mclapply  820.692  932.2262  994.4404 1000.402 1079.930 1117.863    10

sessionInfo()
#R version 3.3.1 (2016-06-21)
#Platform: x86_64-pc-linux-gnu (64-bit)
#Running under: Ubuntu 14.04.5 LTS
#
#locale:
# [1] LC_CTYPE=de_DE.UTF-8       LC_NUMERIC=C               LC_TIME=de_DE.UTF-8        LC_COLLATE=de_DE.UTF-8    
# [5] LC_MONETARY=de_DE.UTF-8    LC_MESSAGES=de_DE.UTF-8    LC_PAPER=de_DE.UTF-8       LC_NAME=C                 
# [9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=de_DE.UTF-8 LC_IDENTIFICATION=C       
#
#attached base packages:
#[1] parallel  stats     graphics  grDevices utils     datasets  methods   base     
#
#other attached packages:
#[1] microbenchmark_1.4-2.1 doParallel_1.0.10      iterators_1.0.8        foreach_1.4.3         
#
#loaded via a namespace (and not attached):
# [1] colorspace_1.2-6 scales_0.4.0     plyr_1.8.4       tools_3.3.1      gtable_0.2.0     Rcpp_0.12.4     
# [7] ggplot2_2.1.0    codetools_0.2-14 grid_3.3.1       munsell_0.4.3