Is rbind + setkey in data.table slower than xts :: rbind which automatically indexes?
What is the reason for data.table
almost 6x slower than xts
updating (= rbind) newlines?
library(quantmod); library(xts); library(data.table)
XTS = getSymbols("AAPL", from="2000-01-01", env = NULL)
# make corresponding `data.table`:
DT <- as.data.table(as.data.frame(XTS))
DT[, Date:=index(XTS)]
setkey(DT,Date)
setcolorder(DT,c("Date",names(XTS)))
# Note: rerun the above before running each test.
system.time(for(i in 1:10) XTS = rbind(XTS, XTS)) # reindexing is automatic
# user system elapsed
# 0.15 0.03 0.47
system.time(for(i in 1:10) DT = setkey(rbind(DT, DT), Date)) # need to manually reset key
# user system elapsed
# 0.64 0.02 2.30
system.time(for(i in 1:10) DT = setkey(rbindlist(list(DT, DT)), Date)) # ditto
# user system elapsed
# 0.60 0.02 2.20
data.table
(as opposed to xts) even gives out memory allocation for i> 15 on my computer.
A common programming use case is when you are doing a time simulation and want to collect intermediate measurements into a table of results that you later want to summarize.
+3
source to share