<- 1e6 n
Pre-Allocating to Improve For Loops
How to pre-allocate vectors used in your for
loops in R
to make your code run faster and perform fewer allocations.
First, set n
to be the size of the vector we want to work with
Then set up a function that doesn’t pre-allocate a list
<- function(n) {
no_alloc
<- list()
x
for (i in seq_len(n)) {
<- i
x[[i]]
}
x }
Then set up a function that does pre-allocate a list
<- function(n) {
pre_alloc
<- vector(mode = "list", length = n)
x
for (i in seq_len(n)) {
<- i
x[[i]]
}
x }
And compare the benchmarks for these functions
library(bench)
<- bench::mark(
res no_alloc(n),
pre_alloc(n)
)
Warning: Some expressions had a GC in every iteration; so filtering is disabled.
c("expression", "min", "median", "itr/sec", "n_gc")] res[
# A tibble: 2 × 4
expression min median `itr/sec`
<bch:expr> <bch:tm> <bch:tm> <dbl>
1 no_alloc(n) 660.4ms 660.4ms 1.51
2 pre_alloc(n) 60.6ms 64.6ms 12.6
We can see that pre-allocating is the way to go!