-
Notifications
You must be signed in to change notification settings - Fork 4
/
benchmark-vector-diff.R
140 lines (125 loc) · 3.27 KB
/
benchmark-vector-diff.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
library(microbenchmark)
library(ggplot2)
library(patchwork)
powers <- 1:8
benchmarks <- list()
for (i in powers) {
cat("Doing", i, "...")
gc()
a <- rnorm(10^i)
benchmarks[[paste("size",
length(a),
sep = ".")]] <-
microbenchmark(
{
b <- numeric() # do not pre-allocate memory
i <- 1
while (i < length(a)) {
b[i] <- a[i+1] - a[i]
i <- i + 1
}
b
},
{
b <- numeric(length(a)-1) # pre-allocate memory
i <- 1
while (i < length(a)) {
b[i] <- a[i+1] - a[i]
i <- i + 1
}
b
},
{
b <- numeric() # do not pre-allocate memory
for(i in seq(along.with = a)) {
b[i] <- a[i+1] - a[i]
}
b
},
{
b <- numeric(length(a)-1) # pre-allocate memory
for(i in seq(along.with = a)) {
b[i] <- a[i+1] - a[i]
}
b
},
{
b <- a[2:length(a)] - a[1:length(a)-1]
b
},
{
b <- diff(a)
b
},
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("while", "while prealloc.",
"for", "for prealloc.",
"extract",
"diff")
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("while", "while prealloc.",
"for", "for prealloc.",
"extract",
"diff")
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.pantera.Rda")
save(diff.benchmark.fig,
fig.seconds,
fig.rel,
summaries,
rel.summaries,
file = "benchmarks.angus.Rda")
# save(diff.benchmark.fig,
# fig.seconds,
# fig.rel,
# summaries,
# rel.summaries,
# file = "benchmarks.carbonilla.Rda")