-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheg_future.R
More file actions
34 lines (28 loc) · 747 Bytes
/
eg_future.R
File metadata and controls
34 lines (28 loc) · 747 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
library(future.apply)
library(tictoc)
# a slow function
slow_square <- function(x = 1) {
x_sq <- x^2
d <- data.frame(value = x, value_squared = x_sq)
Sys.sleep(2)
return(d)
}
# test sequential
plan(sequential)
tic()
seq_ex <- future_lapply(1:4, slow_square)
toc(log = TRUE)
# compare multisession
plan(multisession)
tic()
mse_ex <- future_lapply(1:4, slow_square)
toc(log = TRUE)
# multicore is fastest, but only on *nix systems, else uses sequential
plan(multicore)
tic()
mco_ex <- future_lapply(1:4, slow_square)
toc(log = TRUE)
# check they are identical
stopifnot('seq_ex != mse_ex' = identical(seq_ex, mse_ex))
stopifnot('seq_ex != mco_ex' = identical(seq_ex, mco_ex))
stopifnot('mco_ex != mse_ex' = identical(mco_ex, mse_ex))