Reading ~5x10^6 numeric values into R from a text file is relatively slow on my machine (a few seconds, and I read several such files), even with scan(..., what="numeric", nmax=5000)
or similar tricks. Could it be worthwhile to try an Rcpp
wrapper for this sort of task (e.g. Armadillo
has a few utilities to read text files)?
Or would I likely be wasting my time for little to no gain in performance because of an expected interface overhead? I'm not sure what's currently limiting the speed (intrinsic machine performance, or else?) It's a task that I repeat many times a day, typically, and the file format is always the same, 1000 columns, around 5000 rows.
Here's a sample file to play with, if needed.
nr <- 5000
nc <- 1000
m <- matrix(round(rnorm(nr*nc),3),nr=nr)
cat(m[1, -1], "\n", file = "test.txt") # first line is shorter
write.table(m[-1, ], file = "test.txt", append=TRUE,
row.names = FALSE, col.names = FALSE)
Update: I tried read.csv.sql
and also load("test.txt", arma::raw_ascii)
using Armadillo and both were slower than the scan
solution.
read.csv.sql
in sqldf and see if that is any faster. Its just one line of code. sqldf.googlecode.comsystem.time(b <- read.csv.sql("test.txt", header = FALSE, sep = " "))
and it was slower thansystem.time(a <- scan("test.txt", what="numeric"))
. Also, I think storing the data into a matrix should be more efficient than into adata.frame