-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEfficient_parallel_foreach.R
104 lines (72 loc) · 1.67 KB
/
Efficient_parallel_foreach.R
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# Efficient R Programming
# Running for loops in parallel using foreach
# foreach package by Michelle Wallig and Steve Weston
#### Regression analyses using simulated data ####
set.seed(2020)
data <- data.frame(replicate(200, sample(0:100, 90, replace = TRUE)))
data$result <- sample(1:50, size = 90, replace = TRUE)
IVs <- names(data[-length(data)])
reg <- function(IV) {
model <- as.formula(paste("result ~", IV))
summary(lm(model, data = data))
}
reg("X1")
IV <- "X1"
reg(IV)
#### Base R for loop ####
for (i in 1:length(IVs)) {
IV <- IVs[i]
print(reg(IV))
}
for_seq <- function(IVs) {
models <- vector("list", length(IVs))
for (i in seq_along(IVs)) {
IV <- IVs[i]
models[[i]] <- reg(IV)
}
models
}
for_seq(IVs)
#### foreach, sequentially ####
library(foreach)
foreach (n = IVs) %do%
reg(n)
foreach_seq <- function(IVs) {
foreach (n = IVs) %do%
reg(n)
}
foreach_seq(IVs)
#### foreach, in parallel ####
library(parallel)
cl <- makeCluster(detectCores() - 1)
cl
library(doParallel)
registerDoParallel(cl)
foreach (n = IVs) %dopar%
reg(n)
clusterExport(cl, c("reg", "data"))
foreach_par <- function(IVs) {
foreach (n = IVs) %dopar%
reg(n)
}
foreach_par(IVs)
library(bench)
times <- bench::mark(
for_seq(IVs),
foreach_seq(IVs),
foreach_par(IVs),
iterations = 5
)
times
ggplot2::autoplot(times)
#### Comparing for loops to parallel::clusterApply() ####
times <- bench::mark(
lapply(IVs, reg),
clusterApply(cl, IVs, reg),
foreach_seq(IVs),
foreach_par(IVs),
iterations = 5
)
times
ggplot2::autoplot(times)
stopCluster(cl)