-
Notifications
You must be signed in to change notification settings - Fork 4
/
benchmark-row-sums.R
126 lines (112 loc) · 3.3 KB
/
benchmark-row-sums.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
library(microbenchmark)
library(ggplot2)
library(patchwork)
powers <- c(2, 4, 6, 8) # square matrices
benchmarks <- list()
for (i in powers) {
cat("Doing", i, "...")
gc()
num_rows <- 10^(i %/% 2)
A <- matrix(rnorm(10^i), nrow = num_rows)
benchmarks[[paste("dimension",
paste(dim(A), collapse = "*"),
sep = " ")]] <-
microbenchmark(
{
row.sum <- numeric()
for (i in 1:nrow(A)) {
row.sum[i] <- 0
for (j in 1:ncol(A))
row.sum[i] <- row.sum[i] + A[i, j]
}
},
{
row.sum <- numeric(nrow(A))
for (i in 1:nrow(A)) {
row.sum[i] <- 0
for (j in 1:ncol(A))
row.sum[i] <- row.sum[i] + A[i, j]
}
},
{
row.sum <- numeric()
for (i in 1:nrow(A)) {
row.sum[i] <- sum(A[i, ])
}
},
{
row.sum <- numeric(nrow(A))
for (i in 1:nrow(A)) {
row.sum[i] <- sum(A[i, ])
}
},
{
row.sum <- apply(A, MARGIN = 1, sum)
},
{
row.sum <- rowSums(A)
},
times = c(300L, 300L, 300L, 200L, 100L, 50L, 25L, 12L)[i]
)
cat(" done\n")
}
summaries <- data.frame()
for (b in benchmarks) {
summaries <-
rbind(summaries,
summary(b, unit = "millisecond")[ , c("expr", "median", "cld")])
}
summaries
summaries$size <- rep(10^powers, each = 6)
summaries$loop <- summaries$expr
levels(summaries$loop) <- c("nested for", "nested for alloc.",
"for and sum", "for and sum alloc.",
"apply and sum", "rowSums")
colnames(summaries)
fig.seconds <-
ggplot(summaries, aes(size, median*1e-3, color = loop)) +
geom_point() +
geom_line() +
scale_x_log10(name = "Vector length") +
scale_y_log10(name = "Time (s)") +
scale_color_discrete(name = "Iteration\napproach") +
theme_bw() # + theme(legend.position = "top")
fig.seconds
rel.summaries <- data.frame()
for (b in benchmarks) {
rel.summaries <-
rbind(rel.summaries,
summary(b, unit = "relative")[ , c("expr", "median", "cld")])
}
rel.summaries
rel.summaries$size <- rep(10^powers, each = 6)
rel.summaries$loop <- rel.summaries$expr
levels(rel.summaries$loop) <- c("nested for", "nested for alloc.",
"for and sum", "for and sum alloc.",
"apply and sum", "rowSums")
colnames(rel.summaries)
fig.rel <-
ggplot(rel.summaries, aes(size, median, color = loop)) +
geom_point() +
geom_line() +
scale_x_log10(name = "Vector length") +
scale_y_log10(name = "Time (relative to shortest)",
breaks = c(1, 2, 5, 10, 20, 50, 100, 200, 500, 1000)) +
scale_color_discrete(name = "Iteration\napproach") +
theme_bw() # + theme(legend.position = "none")
fig.rel
diff.benchmark.fig <-
fig.seconds / fig.rel + plot_layout(guides = "collect")
diff.benchmark.fig
# save(diff.benchmark.fig,
# fig.seconds,
# fig.rel,
# summaries,
# rel.summaries,
# file = "benchmarks-rowSums-pantera.Rda")
save(diff.benchmark.fig,
fig.seconds,
fig.rel,
summaries,
rel.summaries,
file = "benchmarks-rowSums-angus.Rda")