random_string <- function() {
paste(sample(letters, 50, replace = TRUE), collapse = "")
}
strings <- replicate(10, random_string())
collapse <- function(xs) {
out <- ""
for (x in xs) {
out <- paste0(out, x)
}
out
}
bench::mark(
collapse(strings),
paste(strings, collapse="")
)
# A tibble: 2 × 6
expression min median `itr/sec` mem_alloc `gc/sec`
<bch:expr> <bch:t> <bch:t> <dbl> <bch:byt> <dbl>
1 "collapse(strings)" 20.88µs 21.96µs 45131. 16KB 18.1
2 "paste(strings, collapse = \"\")" 4.42µs 4.67µs 209927. 0B 0