-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathefficient-R.R
More file actions
327 lines (266 loc) · 7.06 KB
/
efficient-R.R
File metadata and controls
327 lines (266 loc) · 7.06 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
## @knitr system-time
n <- 10000
m <- 1000
x <- matrix(rnorm(n*m), nrow = n)
system.time({
mns <- rep(NA, n)
for(i in 1:n) mns[i] <- mean(x[i , ])
})
system.time(rowMeans(x))
## @knitr microbenchmark
library(microbenchmark)
df <- data.frame(vals = 1:3, labs = c('a','b','c'))
microbenchmark(
df[2,1],
df$vals[2],
df[2, 'vals']
)
## @knitr microbenchmark2
library(microbenchmark)
n <- 1000
x <- matrix(rnorm(n^2), n)
microbenchmark(
t(x) %*% x,
crossprod(x),
times = 10)
## @knitr benchmark
library(rbenchmark)
# speed of one calculation
benchmark(t(x) %*% x,
crossprod(x),
replications = 10,
columns=c('test', 'elapsed', 'replications'))
## @knitr Rprof-fun
lr_slow <- function(y, x) {
xtx <- t(x) %*% x
xty <- t(x) %*% y
inv <- solve(xtx) ## explicit matrix inverse is slow and generally a bad idea numerically
return(inv %*% xty)
}
## @knitr Rprof-run1
## generate random observations and random matrix of predictors
y <- rnorm(5000)
x <- matrix(rnorm(5000*1000), nrow = 5000)
library(proftools)
pd1 <- profileExpr(lr_slow(y, x))
hotPaths(pd1)
hotPaths(pd1, value = 'time')
## @knitr Rprof-run2
lr_medium <- function(y, x) {
xtx <- crossprod(x)
xty <- crossprod(x, y)
inv <- solve(xtx) ## explicit matrix inverse is slow and generally a bad idea numerically
return(inv %*% xty)
}
pd2 <- profileExpr(lr_medium(y, x))
hotPaths(pd2)
hotPaths(pd2, value = 'time')
## @knitr Rprof-run3
lr_fast <- function(y, x) {
xtx <- crossprod(x)
xty <- crossprod(x, y)
U <- chol(xtx)
tmp <- backsolve(U, xty, transpose = TRUE)
return(backsolve(U, tmp))
}
pd3 <- profileExpr(lr_fast(y, x))
hotPaths(pd3)
hotPaths(pd3, value = 'time')
## @knitr Rprof-old
## old approach:
library(fields)
Rprof("makeRegr.prof", interval = 0.005, line.profiling = TRUE)
out <- lr_slow(y, x)
Rprof(NULL)
summaryRprof("makeRegr.prof")
## @knitr preallocate
n <- 10000
z <- rnorm(n)
fun_append <- function(vals) {
x <- exp(vals[1])
n <- length(vals)
for(i in 2:n) x <- c(x, exp(vals[i]))
return(x)
}
fun_prealloc <- function(vals) {
n <- length(vals)
x <- rep(as.numeric(NA), n)
for(i in 1:n) x[i] <- exp(vals[i])
return(x)
}
fun_vec <- function(vals) {
x <- exp(vals)
return(x)
}
benchmark(fun_append(z), fun_prealloc(z), fun_vec(z),
replications = 20, columns=c('test', 'elapsed', 'replications'))
## @knitr init-matrix
nr <- nc <- 2000
benchmark(
x <- matrix(as.numeric(NA), nr, nc),
{x <- as.numeric(NA); length(x) <- nr * nc; dim(x) <- c(nr, nc)},
replications = 10, columns=c('test', 'elapsed', 'replications'))
## @knitr init-list
myList <- vector("list", length = n)
## @knitr vectorize
n <- 1e6
x <- rnorm(n)
benchmark(
x2 <- x^2,
{ x2 <- as.numeric(NA)
length(x2) <- n
for(i in 1:n) { x2[i] <- x[i]^2 } },
replications = 10, columns=c('test', 'elapsed', 'replications'))
## @knitr primitive
`+`
mean.default
chol.default
## @knitr vectorized
address <- c("Four score and seven years ago our fathers brought forth",
" on this continent, a new nation, conceived in Liberty, ",
"and dedicated to the proposition that all men are created equal.")
nchar(address)
# use a vector in the 2nd and 3rd arguments, but not the first
startIndices = seq(1, by = 3, length = nchar(address[1])/3)
startIndices
substring(address[1], startIndices, startIndices + 1)
## @knitr vec-tricks
x <- rnorm(1000000)
benchmark(
truncx <- ifelse(x > 0, x, 0),
{truncx <- x; truncx[x < 0] <- 0},
truncx <- x * (x > 0),
replications = 10, columns=c('test', 'elapsed', 'replications'))
## @knitr apply
n <- 3000; x <- matrix(rnorm(n * n), nr = n)
benchmark(
out <- apply(x, 1, mean),
out <- rowMeans(x),
replications = 10, columns=c('test', 'elapsed', 'replications'))
## @knitr sweep
system.time(out <- sweep(x, 2, STATS = colMeans(x), FUN = "-"))
## @knitr vectorized-sweep
system.time(out2 <- t(t(x) - colMeans(x)))
identical(out, out2)
## @knitr apply-vs-for
n <- 500000; nr <- 10000; nCalcs <- n/nr
mat <- matrix(rnorm(n), nrow = nr)
times <- 1:nr
system.time(
out1 <- apply(mat, 2, function(vec) {
mod = lm(vec ~ times)
return(mod$coef[2])
}))
system.time({
out2 <- rep(NA, nCalcs)
for(i in 1:nCalcs){
out2[i] = lm(mat[ , i] ~ times)$coef[2]
}
})
## @knitr apply-vs-for-part2
z <- rnorm(1e6)
fun_loop <- function(vals) {
x <- as.numeric(NA)
n <- length(vals)
length(x) <- n
for(i in 1:n) x[i] <- exp(vals[i])
return(x)
}
fun_sapply <- function(vals) {
x <- sapply(vals, exp)
return(x)
}
fun_vec <- function(vals) {
x <- exp(vals)
return(x)
}
benchmark(fun_loop(z), fun_sapply(z), fun_vec(z),
replications = 10, columns=c('test', 'elapsed', 'replications'))
## @knitr matrix-calc
mat <- matrix(rnorm(500*500), 500)
benchmark(apply(mat, 1, sum),
mat %*% rep(1, ncol(mat)),
rowSums(mat),
replications = 10, columns=c('test', 'elapsed', 'replications'))
## @knitr linalg-order
n <- 5000
A <- matrix(rnorm(5000 * 5000), 5000)
B <- matrix(rnorm(5000 * 5000), 5000)
x <- rnorm(5000)
system.time(
res1 <- A %*% B %*% x
)
system.time(
res2 <- A %*% (B %*% x)
)
## @knitr diag
n <- 1000
X <- matrix(rnorm(n^2), n)
diagvals <- rnorm(n)
D = diag(diagvals)
# the following lines are very inefficient
summedMat <- X + D
prodMat1 <- D %*% X
prodMat2 <- X %*% D
# How can we do each of those operations much more quickly?
## @knitr match-lookup
df <- data.frame(
id = 1:5,
clusterLabel = c('C', 'B', 'B', 'A', 'C'))
info <- data.frame(
grade = c('A', 'B', 'C'),
numGrade = c(95, 85, 75),
fail = c(FALSE, FALSE, TRUE) )
grp <- match(df$clusterLabel, info$grade)
df$numGrade <- info$numGrade[grp]
df
## @knitr name-lookup
info2 <- info$numGrade
names(info2) <- info$grade
info2
info2[df$clusterLabel]
## @knitr index-lookup
n <- 1000
x <- 1:n
xL <- as.list(x)
nms <- paste0("var", as.character(x))
names(x) <- nms
names(xL) <- nms
x[1:3]
xL[1:3]
microbenchmark(
x[500], # index lookup in vector
x["var500"], # name lookup in vector
xL[[500]], # index lookup in list
xL[["var500"]]) # name lookup in list
## @knitr env-lookup
xEnv <- as.environment(xL) # convert from a named list
xEnv$var500
microbenchmark(
x[500],
xL[[500]],
xEnv[["var500"]],
xEnv$var500
)
## @knitr cache-aware
nr <- 800000
nc <- 100
## large matrix that won't fit in cache
A <- matrix(rnorm(nr * nc), nrow = nr)
tA <- t(A)
benchmark(
apply(A, 2, mean), ## operate by column
apply(tA, 1, mean), ## exact same calculation, but by row
replications = 10, columns=c('test', 'elapsed', 'replications'))
## @knitr cache-aware2
nr <- 800
nc <- 100
## small matrix that should fit in cache
A <- matrix(rnorm(nr * nc), nrow = nr)
## Yep, the size is less than the L3 cache:
object.size(A)
memuse::Sys.cachesize()
tA <- t(A)
benchmark(apply(A, 2, mean), ## by column
apply(tA, 1, mean), ## by row
replications = 10, columns=c('test', 'elapsed', 'replications'))