splitstackshape icon indicating copy to clipboard operation
splitstackshape copied to clipboard

Test pasted strings vs tempfiles

Open mrdwab opened this issue 6 years ago • 0 comments

funpaste <- function(invec, sep = ",") {
  VEC <- as.character(invec)
  ana <- if (anyNA(VEC)) is.na(VEC) else NULL
  anb <- !nzchar(VEC)
  if (is.null(ana) & !any(anb)) {
    VEC <- if (requireNamespace("stringi", quietly = TRUE)) {
      stringi::stri_flatten(VEC, collapse = "\n")
    } else {
      # nocov start
      .strflat(VEC)
      # nocov end
    }
  } else {
    if (!is.null(ana)) VEC[which(ana)] <- sep
    if (any(anb)) VEC[which(anb)] <- sep
    VEC <- if (requireNamespace("stringi", quietly = TRUE)) {
      stringi::stri_flatten(VEC, collapse = "\n")
    } else {
      # nocov start
      .strflat(VEC)
      # nocov end
    }
  }
  fread(VEC, sep = sep, fill = TRUE, 
             blank.lines.skip = FALSE, header = FALSE,
             encoding = "UTF-8", logical01 = FALSE)
}

funtemp <- function(invec) {
  on.exit(rm(a))
  on.exit(gc())
  a <- tempfile()
  fwrite(as.data.table(invec), file = a, quote = FALSE, nThread = 4)
  fread(a, fill = TRUE, skip = 1, header = FALSE, nThread = 4)
}

library(microbenchmark)
microbenchmark(funpaste(x), funtemp(x))

funpaste(y)

funtemp(y)

mrdwab avatar Apr 13 '18 12:04 mrdwab