Skip to content

Instantly share code, notes, and snippets.

@erictleung
Created October 30, 2016 21:34
Show Gist options
  • Save erictleung/4400eb93f8b7e6e8146601abfddf3a9b to your computer and use it in GitHub Desktop.
Save erictleung/4400eb93f8b7e6e8146601abfddf3a9b to your computer and use it in GitHub Desktop.
Example code to parallelize R with plyr and doMC
# Parallelize R
# Source: http://blog.yhat.com/posts/running-r-in-parallel.html
library(plyr)
library(doMC)
# set number of cores (mine has two cores)
doMC::registerDoMC(cores = 2) # number of cores you have access to
# Process data
# don't parallelize
system.time(ddply(iris, .(Species), function(x) {
Sys.sleep(2)
nrow(x)
}))
# user system elapsed
# 0.030 0.022 6.015
# parallelize
system.time(ddply(iris, .(Species), function(x) {
Sys.sleep(2)
nrow(x)
}, .parallel = TRUE))
# user system elapsed
# 0.016 0.025 4.033
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment