Skip to content

Instantly share code, notes, and snippets.

@dpastoor
Created August 10, 2016 15:21
Show Gist options
  • Select an option

  • Save dpastoor/98ffc68da4dba3198e061bb31633fdae to your computer and use it in GitHub Desktop.

Select an option

Save dpastoor/98ffc68da4dba3198e061bb31633fdae to your computer and use it in GitHub Desktop.
future example for parallel processing very raw
library(microbenchmark)
library(tibble)
library(future)
library(dplyr)
nids <- 16
sleep_times <- data_frame(REP = 1:nids, sleep_time = round(runif(nids, 0, 2), 0))
sleep_times
eager_bm <- function(sleep_times) {
plan(eager)
f <- list()
for (ii in 1:nrow(sleep_times)) {
f[[ii]] <- future({
message("a message inside an eager future")
sleep_time <- slice(sleep_times, ii)[[2]]
start_time <- Sys.time()
Sys.sleep(sleep_time)
list(sleep_time = sleep_time,
start_time = start_time,
end_time = Sys.time(),
pid = Sys.getpid())
})
}
v <- lapply(f, FUN = value)
bind_rows(v)
}
multiprocess_bm <- function(sleep_times) {
plan(multiprocess)
f <- list()
for (ii in 1:nrow(sleep_times)) {
f[[ii]] <- future({
message("a message inside a multiprocess future, you'll never see me")
sleep_time <- slice(sleep_times, ii)[[2]]
start_time <- Sys.time()
Sys.sleep(sleep_time)
list(sleep_time = sleep_time,
start_time = start_time,
end_time = Sys.time(),
pid = Sys.getpid())
})
}
v <- lapply(f, FUN = value)
bind_rows(v)
}
data<- sleep_times
# 4x faster for multiprocess on MBP
res10x <- microbenchmark::microbenchmark(
eager_bm(data),
multiprocess_bm(data),
times=10L
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment