Skip to content

Instantly share code, notes, and snippets.

@gadenbuie
Last active June 12, 2019 00:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gadenbuie/9f468531d01a0bdc90e9b9353bdfe39a to your computer and use it in GitHub Desktop.
Save gadenbuie/9f468531d01a0bdc90e9b9353bdfe39a to your computer and use it in GitHub Desktop.
library(tibble)

string1 <- sample(letters, 10000, replace = TRUE)
string2 <- sample(LETTERS, 10000, replace = TRUE)
listcol <- lapply(1:10000, function(x) sample(LETTERS, 5))

df_list <- list(s1 = string1, s2 = string2)


new_minimal_tibble <- function(...) {
  x <- list(...)
  n_row = unique(unlist(lapply(x, length)))
  if (length(n_row) == 1) {
    tibble::new_tibble(x, nrow = n_row)
  } else {
    tibble::as_tibble(x)
  }
}

bench::mark(
  data.frame(s1 = string1, s2 = string2),
  as.data.frame(df_list),
  new_tibble(list(s1 = string1, s2 = string2), nrow = 10000),
  new_tibble(df_list, nrow = 10000),
  new_minimal_tibble(s1 = string1, s2 = string2),
  validate_tibble(tibble::new_tibble(list(s1 = string1, s2 = string2), nrow = 10000)),
  tibble(s1 = string1, s2 = string2),
  as_tibble(df_list),
  new_tibble(list(s1 = string2, list_col = listcol), nrow = 10000),
  check = FALSE
)[, c(1:2, 4:6)]
#> # A tibble: 9 x 5
#>   expression                                min   median      max `itr/sec`
#>   <chr>                                <bch:tm> <bch:tm> <bch:tm>     <dbl>
#> 1 data.frame(s1 = string1, s2 = strin… 855.25µs 983.82µs      2ms      955.
#> 2 as.data.frame(df_list)               875.25µs 932.99µs   2.84ms     1004.
#> 3 new_tibble(list(s1 = string1, s2 = …   5.77µs   6.37µs  41.94µs   147844.
#> 4 new_tibble(df_list, nrow = 10000)      5.53µs   6.15µs  32.73µs   152813.
#> 5 new_minimal_tibble(s1 = string1, s2…  23.99µs  25.62µs   6.71ms    34731.
#> 6 validate_tibble(tibble::new_tibble(… 134.25µs 140.24µs 800.66µs     6497.
#> 7 tibble(s1 = string1, s2 = string2)   331.19µs 347.31µs   1.42ms     2657.
#> 8 as_tibble(df_list)                   209.04µs 222.61µs   1.12ms     4082.
#> 9 new_tibble(list(s1 = string2, list_…   5.81µs   6.73µs  92.46µs   127274.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment