Skip to content

Instantly share code, notes, and snippets.

@mrdwab
Created February 15, 2015 15:40
Show Gist options
  • Select an option

  • Save mrdwab/d4b30946b82cd3515ed1 to your computer and use it in GitHub Desktop.

Select an option

Save mrdwab/d4b30946b82cd3515ed1 to your computer and use it in GitHub Desktop.
set.seed(1)
n <- 100000
l <- cbind(sequence(n), sample(c(letters, LETTERS), n, TRUE))
l <- split(l, 1:nrow(l))
library(microbenchmark)
library(stringi)
rb <- function() do.call(rbind, l)
rb2 <- function() {
A <- data.frame(do.call(rbind, l), stringsAsFactors = FALSE)
A[] <- lapply(A, type.convert)
A
}
rbdf <- function() setNames(do.call(rbind.data.frame, l), c("col1", "col2"))
s2a <- function() t(simplify2array(l))
s2a2 <- function() {
A <- data.frame(t(simplify2array(l)), stringsAsFactors = FALSE)
A[] <- lapply(A, type.convert)
A
}
sl2m <- function() stri_list2matrix(l, byrow = TRUE)
sl2m2 <- function() {
A <- data.frame(stri_list2matrix(l, byrow = TRUE), stringsAsFactors = FALSE)
A[] <- lapply(A, type.convert)
A
}
microbenchmark(rb(), rb2(), rbdf(), s2a(), s2a2(), sl2m(), sl2m2(), times = 10)
# Unit: milliseconds
# expr min lq mean median uq max neval
# rb() 2107.037607 2126.034404 2189.736374 2168.413060 2270.935747 2304.998340 10
# rb2() 2167.301232 2179.369576 2252.435357 2196.388378 2220.951455 2737.306016 10
# rbdf() 3760.737631 3846.041151 4050.422107 4006.229182 4193.175611 4576.599444 10
# s2a() 270.689418 290.114168 342.303776 306.092319 312.439534 674.295284 10
# s2a2() 326.833278 365.212878 433.037203 380.049525 437.508256 861.766501 10
# sl2m() 5.533927 5.655753 6.034395 5.733178 6.801719 7.028414 10
# sl2m2() 25.641438 26.191216 61.006942 37.640613 54.172781 247.672368 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment