Skip to content

Instantly share code, notes, and snippets.

@sinarueeger
Created February 25, 2019 15:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sinarueeger/197395658c61d85e733bcdf44f71124f to your computer and use it in GitHub Desktop.
Save sinarueeger/197395658c61d85e733bcdf44f71124f to your computer and use it in GitHub Desktop.
Using the future package in R for parallel computing
## //////////////////
## FUTURE PACKAGE
## //////////////////
## here is a function that sums up really slow
## ---------------------------------------------
slow_sum <- function(vec) {
SUM <- 0
for (i in vec) {
SUM <- SUM + i
Sys.sleep(0.2)
}
return(SUM)
}
## r package needed for microbenchmarking
library(microbenchmark)
## parameter
vec <- 1:20
## 1) traditional computation
## -------------------------
microbenchmark(lapply(1:20, FUN = slow_sum), times=10, unit = "us") ## 4'002'902
## 2) using parallel package
## -------------------------
microbenchmark(parallel::mclapply(1:20, FUN = slow_sum), times=10, unit = "us") ## 710'882
## 3) how future works
## -------------------------
library(future)
plan(sequential)
tmp <- future(slow_sum(vec))
fa <- value(tmp)
## is identical to
fa %<-% slow_sum(vec)
## 4) using different plans
## -------------------------
?plan
## sequential
plan(sequential)
microbenchmark(fa %<-% future( slow_sum(vec) ), times=10, unit = "us")
## parallelel
plan(multiprocess)
microbenchmark(fb %<-% future( slow_sum(vec) ), times=10, unit = "us")
## multiprocess will use the maximal available cores
future::availableCores("mc.cores")
## 5) other options that use future as a backbone: future.apply and furrr
## -------------------------
#microbenchmark(future.apply::future_lapply(1:20, FUN = slow_sum), times=10, unit = "us") ## 1'160'609
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment