Last active
November 10, 2022 20:11
-
-
Save hadley/48c396ae674cdccb618033df1b1ed671 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
f1 <- function(n) { | |
x <- numeric() | |
for (i in 1:n) { | |
x <- c(x, i) | |
} | |
x | |
} | |
f1.5 <- function(n) { | |
x <- numeric() | |
for (i in 1:n) { | |
x[[i]] <- i | |
} | |
x | |
} | |
f2 <- function(n) { | |
x <- numeric(n) | |
for (i in 1:n) { | |
x[[i]] <- i | |
} | |
x | |
} | |
bench::mark(f1(1e4), f1.5(1e4), f2(1e4)) | |
#> Warning: Some expressions had a GC in every iteration; so filtering is disabled. | |
#> # A tibble: 3 × 6 | |
#> expression min median `itr/sec` mem_alloc `gc/sec` | |
#> <bch:expr> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl> | |
#> 1 f1(10000) 109ms 113.34ms 8.77 382MB 77.2 | |
#> 2 f1.5(10000) 936µs 1.05ms 726. 1.68MB 67.6 | |
#> 3 f2(10000) 212µs 216.77µs 4495. 96.77KB 6.00 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I switched both to
[[
; as I'd expect, that made very little difference.