Skip to content

Instantly share code, notes, and snippets.

@johndrummond
Created April 8, 2019 14:16
Show Gist options
  • Save johndrummond/a1b287ae6a10b0966cb6992940e4aca8 to your computer and use it in GitHub Desktop.
Save johndrummond/a1b287ae6a10b0966cb6992940e4aca8 to your computer and use it in GitHub Desktop.
Simple comparison of performance of lookups of environments vs lists in R
# lookup_list_vs_env.R
# comparing the performance of lookups with lists as opposed to
# environments in R. As the size of the collection increases, the better
# performance of the environments for lookups is multiples better.
library(microbenchmark)
library(purrr)
library(stringr)
e1 <- new.env()
l1 <- list()
walk(1:200, ~{
keytxt <- str_c("key",.x)
valtxt <- str_c("#DD25",.x)
e1[[keytxt]] <- valtxt
l1[keytxt] <<- valtxt
})
x <- sample(names(e1),100,TRUE)
microbenchmark(
walk(x,~e1[[.x]]),
walk(x,~l1[[.x]])
)
# e.g.
# Unit: microseconds
# expr min lq mean median uq max neval
# walk(x, ~e1[[.x]]) 152.400 155.2515 168.497 157.051 159.4015 868.102 100
# walk(x, ~l1[[.x]]) 226.002 229.4515 247.902 231.701 235.3010 1284.001 100
walk(1:1000, ~{
keytxt <- str_c("key",.x)
valtxt <- str_c("#DD25",.x)
e1[[keytxt]] <- valtxt
l1[keytxt] <<- valtxt
})
x <- sample(names(e1),100,TRUE)
microbenchmark(
walk(x,~e1[[.x]]),
walk(x,~l1[[.x]]),
times=1000
)
# e.g.
# Unit: microseconds
# expr min lq mean median uq max neval
# walk(x, ~e1[[.x]]) 154.001 163.151 204.752 170.1015 187.3015 8936.901 1000
# walk(x, ~l1[[.x]]) 540.401 553.401 599.476 561.7005 583.7010 4437.400 1000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment