You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
shift() in data.table v1.9.6 is slow for many groups #1534
+1 for this performance enhancement. shift() is the main bottleneck in a lot of my code. Seems that for a fixed number of rows, the time it takes to run shift() is proportional to the number of groups in the data.
library(data.table)
# Build table to store timings
timings <- CJ(RowCount = 10^7, Groups = 10^c(0:7))
timings[, SizePerGroup := RowCount/Groups]
# Loop through each experiment
for(i in 1:nrow(dt)){
print(paste0("Iteration: ", i))
# Build dataset
timings_i <- timings[i]
dt <- data.table(Grp = rep(seq_len(timings_i$Groups), each = timings_i$SizePerGroup))
dt[, Value := sample(100, size = .N, replace = T)]
# Measure the time it takes to insert a column indicating the previous value by group
elapsed <- system.time(dt[, PrevValueByGrp := shift(Value, type = "lag"), by = Grp])["elapsed"]
timings[i, Elapsed := elapsed]
}
library(ggplot2)
ggplot(timings, aes(x = Groups, y = Elapsed))+geom_line()+geom_point()
Activity
arunsrinivasan commentedon Mar 3, 2016
That's not surprising. This'll go away when
gforceis optimised for:=. It's on the list for this release, I believe.ben519 commentedon Nov 10, 2018
+1 for this performance enhancement.
shift()is the main bottleneck in a lot of my code. Seems that for a fixed number of rows, the time it takes to runshift()is proportional to the number of groups in the data.franknarf1 commentedon Nov 10, 2018
@ben519 Fyi, for the special case of when your code looks like that, there's a shortcut: