-
Notifications
You must be signed in to change notification settings - Fork 59
/
benchmarks.Rmd
136 lines (106 loc) · 3.01 KB
/
benchmarks.Rmd
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
---
title: "Benchmarks"
output:
github_document:
toc: true
---
Some rough benchmarks for testing how long things take...
## Setup
```{r setup}
library(tidyverse)
library(tidybayes)
```
## `spread/gather_draws`
```{r}
k = 50000
set.seed(1234)
df = data.frame(
.chain = NA,
.iteration = NA,
.draw = 1:k,
`a[2]` = rnorm(k),
`b[2]` = rnorm(k),
`c[2]` = rnorm(k),
`d[2]` = rnorm(k),
`e[2]` = rnorm(k),
`f[2]` = rnorm(k),
`g[2]` = rnorm(k),
`h[2]` = rnorm(k),
`i[2]` = rnorm(k),
`j[2]` = rnorm(k),
`k[2]` = rnorm(k),
`l[2]` = rnorm(k),
stringsAsFactors = FALSE,
check.names = FALSE
) %>%
as_tibble()
df
```
### Spreading with indices
This should be fairly fast. Last test was ~= 0.03 seconds on `monarch`.
```{r}
system.time(spread_draws(df, `.`[index], regex = TRUE))
```
#### Separate-spec version
This is definitely slower than it could be, likely due to use of joins with chain information involved. This could almost
definitely be made faster if nesting was used all the way up the spread_draws pipeline and chain information added
at the last possible moment. Last test was ~= 0.35 seconds on `monarch`.
```{r}
system.time(spread_draws(df,
a[index], b[index], c[index], d[index], e[index], f[index],
g[index], h[index], i[index], j[index], k[index], l[index]
))
```
#### Using gather_draws
This is slower than the spread_draws version even though it shouldn't really have to be. This is a result of spread_draws_long_ doing a spread that is later undone by gather_draws. Could either use a flag to skip this (although that is a little complicated as the `|` syntax might need the spread anyway) or rely on switching to nesting throughout the entire pipeline to just make the spread/gather round trip not so costly. Last test was ~= 0.13 seconds on `monarch`.
```{r}
system.time(gather_draws(df, `.`[index], regex = TRUE))
```
### Spread with a lot of levels in an index
```{r}
k = 2500
l = 40
nuisance = 9000 - l
set.seed(1234)
ldf = tibble(
.chain = NA,
.iteration = NA,
.draw = 1:k
)
for (i in 1:l) {
ldf[[paste0("a[", i, "]")]] = rnorm(k)
ldf[[paste0("b[", i, "]")]] = rnorm(k)
}
for (i in 1:nuisance) {
ldf[[paste0("p", i)]] = rnorm(k)
}
head(ldf[,1:10])
```
```{r}
lmcmc = coda::mcmc.list(
coda::mcmc(select(ldf, -.chain, -.iteration, -.draw)),
coda::mcmc(select(ldf, -.chain, -.iteration, -.draw))
)
```
Faster than it used to be, but still slow. Last test was ~.77 on monarch.
```{r}
system.time(tidy_draws(lmcmc))
```
Last test was ~.95 on monarch
```{r}
system.time(spread_draws(lmcmc, a[i], b[i]))
```
Note how a lot of the slowdown here is due to tidy_draws. Last test was ~.08 on monarch.
```{r}
system.time(spread_draws(ldf, a[i], b[i]))
```
### Array columns
This tends to be slower than spreading without array columns. Last test was ~0.08 seconds on `monarch`.
```{r}
data(RankCorr, package = "ggdist")
system.time(spread_draws(RankCorr, b[.,.]))
```
And this is currently very slow. Last test was ~0.55 seconds on `monarch`.
```{r}
system.time(spread_draws(df, a[.]))
```