Created
September 6, 2013 10:49
-
-
Save rpietro/6462284 to your computer and use it in GitHub Desktop.
simple parallelization example
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
# part of this code from Thiele's http://goo.gl/Ed1zrq | |
# call packages | |
library(parallel) | |
library(microbenchmark) | |
#create simple function | |
testfun1 <- function(x) { | |
return(x*x) | |
} | |
my.v1 <- 1:10 | |
print(my.v1) | |
my.v1.quad <- sapply(my.v1, testfun1) | |
print(my.v1.quad) | |
# detect the number of cores available | |
processors <- detectCores() | |
processors | |
# create cluster | |
cl <- makeCluster(processors) | |
# call parallel sapply | |
my.v1.quad.par <- parSapply(cl, my.v1, testfun1) | |
print(my.v1.quad.par) | |
#microbenchmark | |
compared <- microbenchmark(my.v1.quad, my.v1.quad.par, times=1000L) | |
print(compared) | |
boxplot(compared) | |
# stop cluster | |
stopCluster(cl) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment