Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test pasted strings vs tempfiles #60

Open
mrdwab opened this issue Apr 13, 2018 · 0 comments
Open

Test pasted strings vs tempfiles #60

mrdwab opened this issue Apr 13, 2018 · 0 comments

Comments

@mrdwab
Copy link
Owner

mrdwab commented Apr 13, 2018

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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant