Skip to content

Instantly share code, notes, and snippets.

@akiatoji
Created July 7, 2015 22:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akiatoji/eaafffd6929f1cc13395 to your computer and use it in GitHub Desktop.
Save akiatoji/eaafffd6929f1cc13395 to your computer and use it in GitHub Desktop.
Benchmark parallel R backends
# Quick and dirty comparison of parallelism in R
#import packages
library(doMC)
library(doMPI)
library(foreach)
library(doParallel)
#number of iterations and procs
iters<-1e4
procs <- 2
###################################################
# Run with stock doParallel
cl<-makeCluster(procs)
registerDoParallel(cl)
strt<-Sys.time()
ls<-foreach(icount(iters)) %dopar% {
to.ls<-rnorm(1e6)
to.ls<-summary(to.ls)
to.ls
}
print("Parallel Procs")
print(Sys.time()-strt)
stopCluster(cl)
###################################################
# Run with doMC (supposingly subsumed into stock R)
registerDoMC(procs)
strt<-Sys.time()
ls<-foreach(icount(iters)) %dopar% {
to.ls<-rnorm(1e6)
to.ls<-summary(to.ls)
to.ls
}
print("Parallel Core")
print(Sys.time()-strt)
###################################################
# Run with doMPI
cl <- startMPIcluster(procs)
registerDoMPI(cl)
strt<-Sys.time()
ls<-foreach(icount(iters)) %dopar% {
to.ls<-rnorm(1e6)
to.ls<-summary(to.ls)
to.ls
}
print("Parallel MPI")
print(Sys.time()-strt)
closeCluster(cl)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment