Created
October 30, 2016 21:34
-
-
Save erictleung/4400eb93f8b7e6e8146601abfddf3a9b to your computer and use it in GitHub Desktop.
Example code to parallelize R with plyr and doMC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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