Test of different version of parallel computing

In conclusion, if no progress bar is necessary on unix system, mclapply with mc.preschedule = TRUE is very good. Don't forget to indicate mc.cores because the default is only 2 cores.
If you work in windows system, you must use parLapply. However it is more complicated as you must transfer variable and which packages must be loaded.



# No parallel computing; give a reference
st1 <- system.time(f <- lapply(1:10000, FUN=function(x) {Sys.sleep(0.001)}))

library(parallel)
# Parallel computing using 4 cores with fork and mc.preschedule being TRUE
st2 <- system.time(f <- mclapply(1:10000, FUN=function(x) {Sys.sleep(0.001)}, mc.cores = 4))
# Parallel computing using 4 cores with fork and mc.preschedule being FALSE
st3 <- system.time(f <- mclapply(1:10000, FUN=function(x) {Sys.sleep(0.001)}, mc.cores = 4, mc.preschedule = FALSE))

# Parallel computing using 4 cores without fork
st4 <- system.time({cl <- parallel::makeCluster(4); 
f <- parLapply(cl = cl, X=1:10000, fun=function(x) {Sys.sleep(0.001)}); 
stopCluster(cl)})


# Parallel computing using 4 cores with forking and a progress bar
library(pbapply)
st5 <- system.time(f <- pblapply(1:10000, FUN=function(x) {Sys.sleep(0.001)}, cl = 4))
# Parallel computing using 4 cores without forking and a progress bar
st6 <- system.time({cl <- parallel::makeCluster(4); 
f <- pblapply(1:10000, FUN=function(x) {Sys.sleep(0.001)}, cl = cl);
stopCluster(cl)})

# Parallel computing using 4 cores with forking and a progress bar
library(pbmcapply)
st7 <- system.time(f <- pbmclapply(1:10000, FUN=function(x) {Sys.sleep(0.001)}, mc.cores = 4))

k <- rbind(st1, st2, st3, st4, st5, st6, st7)
rownames(k) <- c("lapply",
                 "mclapply, precheduleT", 
                 "mclapply, precheduleF", 
                 "parLapply", 
                 "pblapply, forkT", 
                 "pblapply, forkF", 
                 "pbmlapply")
k


Response:

                      user.self sys.self elapsed user.child sys.child
lapply                    0.419    0.297  13.160      0.000     0.000
mclapply, precheduleT     0.010    0.020   3.226      0.353     0.407
mclapply, precheduleF     9.050   22.169  51.082     40.007    84.247
parLapply                 0.017    0.015   4.525      0.005     0.006
pblapply, forkT           0.320    1.631   5.219      1.009     2.095
pblapply, forkF           0.276    0.075   4.900      0.005     0.007
pbmlapply                 1.446    0.309   3.258      0.000     0.000

Commentaires

Posts les plus consultés de ce blog

Standard error from Hessian Matrix... what can be done when problem occurs

stepAIC from package MASS with AICc

Install treemix in ubuntu 20.04