Skip to content

Instantly share code, notes, and snippets.

@rpietro
Created September 6, 2013 10:49
Show Gist options
  • Save rpietro/6462284 to your computer and use it in GitHub Desktop.
Save rpietro/6462284 to your computer and use it in GitHub Desktop.
simple parallelization example
# 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