Skip to content

Instantly share code, notes, and snippets.

@krlmlr
Created October 11, 2023 04:43
Show Gist options
  • Save krlmlr/b9ae8c3025cfbee9fc84258f98cdb211 to your computer and use it in GitHub Desktop.
Save krlmlr/b9ae8c3025cfbee9fc84258f98cdb211 to your computer and use it in GitHub Desktop.
Comparing single- and double-bracket subsetting for atomic vectors
my_letters <- c("A", "B", "C", "D", "E")
bench::mark(for (i in 1:10000) NULL)
#> # A tibble: 1 × 6
#> expression min median `itr/sec` mem_alloc `gc/sec`
#> <bch:expr> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl>
#> 1 for (i in 1:10000) NULL 312µs 332µs 2972. 38.9KB 36.3
bench::mark(for (i in 1:10000) my_letters[3], for (i in 1:10000) my_letters[[3]])
#> # A tibble: 2 × 6
#> expression min median `itr/sec` mem_alloc `gc/sec`
#> <bch:expr> <bch:t> <bch:> <dbl> <bch:byt> <dbl>
#> 1 for (i in 1:10000) my_letters[3] 941µs 979µs 1010. 10.25KB 85.6
#> 2 for (i in 1:10000) my_letters[[3]] 933µs 988µs 1003. 8.16KB 82.0
y <- 3
bench::mark(for (i in 1:10000) my_letters[y], for (i in 1:10000) my_letters[[y]])
#> # A tibble: 2 × 6
#> expression min median `itr/sec` mem_alloc `gc/sec`
#> <bch:expr> <bch:t> <bch:> <dbl> <bch:byt> <dbl>
#> 1 for (i in 1:10000) my_letters[y] 884µs 925µs 1068. 9.48KB 69.4
#> 2 for (i in 1:10000) my_letters[[y]] 844µs 893µs 1112. 8.43KB 75.2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment