-
Notifications
You must be signed in to change notification settings - Fork 3
/
timing_comparison.Rmd
167 lines (143 loc) · 4.92 KB
/
timing_comparison.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
---
output:
github_document:
html_preview: false
---
```{r, echo = FALSE}
knitr::opts_chunk$set(
error = TRUE,
cache = TRUE,
cache.vars = 'timings',
fig.path = 'timing-'
)
```
# Comparing efficiency with `geohash` package
I mentioned in the [`README`](README.md) that `gh_encode` is fast; how does it compare to the [`geohash`](https://github.com/Ironholds/geohash) package (unfortunately, this package was removed from CRAN, which is a big part of the motivation for making this package in the first place)?
Here is a small set of timing exercises (note -- this may take several minutes to reproduce on your machine)
## Tools and setup
```{r setup}
library(microbenchmark)
library(data.table)
nn = 10^(0:6)
kk = 1:8
timings = CJ(n_rows = nn, precision = kk)
setkey(timings)
```
## Encoding geohashes
```{r encoding}
set.seed(49843)
# generate random points on the Earth's surface
xx = runif(max(nn), -180, 180)
yy = runif(max(nn), -90, 90)
for (n in nn) {
x = xx[1:n]
y = yy[1:n]
times = if (n > 1e5) 10L else 100L
for (k in kk) {
# don't need as many repetitions at higher n
p = summary(microbenchmark(times = times,
geohash::gh_encode(y, x, k),
geohashTools::gh_encode(y, x, k)),
unit = 'ms')
timings[.(n, k), c('encode_geohashTools', 'encode_relative') :=
.(p[2L, 'median'], p[1L, 'median']/p[2L, 'median'])]
}
}
```
## Decoding geohashes
```{r decoding}
set.seed(459849)
base32 = strsplit('0123456789bcdefghjkmnpqrstuvwxyz', NULL)[[1L]]
ggghhh = sapply(integer(max(nn)), function(...)
paste(sample(base32, max(kk), replace = TRUE), collapse = ''))
for (n in nn) {
gghh = ggghhh[1:n]
times = if (n > 1e5) 10L else 100L
for (k in kk) {
gh = substring(gghh, 1L, k)
p = summary(microbenchmark(times = times,
geohash::gh_decode(gh),
geohashTools::gh_decode(gh)),
unit = 'ms')
timings[.(n, k), c('decode_geohashTools', 'decode_relative') :=
.(p[2L, 'median'], p[1L, 'median']/p[2L, 'median'])]
}
}
```
## Finding geohash neighbors
```{r neighbors}
# re-use ggghhh from above
for (n in nn) {
gghh = ggghhh[1:n]
times = if (n > 1e5) 10L else 100L
for (k in kk) {
gh = substring(gghh, 1L, k)
p = summary(microbenchmark(times = times,
geohash::gh_neighbours(gh),
geohashTools::gh_neighbors(gh)),
unit = 'ms')
timings[.(n, k), c('neighbors_geohashTools', 'neighbors_relative') :=
.(p[2L, 'median'], p[1L, 'median']/p[2L, 'median'])]
}
}
```
# Results
## Absolute timing
```{r reset_dt, cache = FALSE, echo = FALSE, message = FALSE}
library(data.table)
setDT(timings)
```
```{r absolute_results, cache = FALSE, results = 'hide'}
par(oma = c(0, 0, 2, 0))
timings[ , {
by = 'n_rows'
xlab_map = c(n_rows = '# Rows', precision = 'Geohash Precision')
log_map = c(n_rows = 'xy', precision = 'y')
main_map = c(n_rows = 'Across Input Size',
precision = 'Across Geohash Precision')
rel_cols = paste0(c('encode', 'decode', 'neighbors'), '_geohashTools')
par(mfrow = 1:2)
for (by in c('n_rows', 'precision')) {
.SD[ , lapply(.SD, mean), keyby = by, .SDcols = rel_cols
][ , {
y = as.matrix(.SD[ , !by, with = FALSE])
matplot(.SD[[1L]], y, type = 'l', lty = 1L, lwd = 3L,
log = log_map[by], xlab = xlab_map[by], col = 2:4,
las = 1L, main = main_map[by],
ylab = 'Average Time (ms)')
if (by == 'n_rows')
legend('topleft', col = 2:4, lwd = 3L,
legend = c('Encoding', 'Decoding', 'Neighbors'))
}]
title('Absolute Performance', outer = TRUE)
}
}]
```
## Comparison against `geohash`: Relative timing
```{r relative_results, cache = FALSE, results = 'hide'}
par(oma = c(0, 0, 2, 0))
timings[ , {
by = 'n_rows'
xlab_map = c(n_rows = '# Rows', precision = 'Geohash Precision')
log_map = c(n_rows = 'xy', precision = 'y')
main_map = c(n_rows = 'Across Input Size',
precision = 'Across Geohash Precision')
rel_cols = paste0(c('encode', 'decode', 'neighbors'), '_relative')
par(mfrow = 1:2)
for (by in c('n_rows', 'precision')) {
.SD[ , lapply(.SD, mean), keyby = by, .SDcols = rel_cols
][ , {
y = as.matrix(.SD[ , !by, with = FALSE])
matplot(.SD[[1L]], y, type = 'l', lty = 1L, lwd = 3L,
log = log_map[by], xlab = xlab_map[by], col = 2:4,
las = 1L, main = main_map[by],
ylab = 'Relative Timing: geohash/geohashTools')
if (by == 'n_rows')
legend('topright', col = 2:4, lwd = 3L,
legend = c('Encoding', 'Decoding', 'Neighbors'))
abline(h = 1, lty = 2L, lwd = 1L)
}]
title('Relative Performance', outer = TRUE)
}
}]
```