Data.table: shift () em data.table v1.9.6 é lento para muitos grupos

Criado em 11 fev. 2016  ·  3Comentários  ·  Fonte: Rdatatable/data.table

Olá!
Para muitos grupos diferentes em by , shift é muito mais lento do que o deslocamento manual.
Consulte: http://stackoverflow.com/questions/35179911/shift-in-data-table-v1-9-6-is-slow-for-many-groups
e https://github.com/nachti/datatable_test/blob/master/leadtest.R para um exemplo detalhado.
Saúde,
Gerhard

GForce performance

Comentários muito úteis

@ ben519 Fyi, para o caso especial de quando seu código for assim, há um atalho:

library(data.table)
dt <- data.table(Grp = rep(seq_len(1e6), each=10L))
dt[, Value := sample(100L, size = .N, replace = TRUE)]

system.time(dt[, PrevValueByGrp := shift(Value, type = "lag"), by = Grp][])
#    user  system elapsed 
#   19.50    0.80   20.34
system.time(dt[, v := shift(Value, type = "lag")][rowid(Grp)==1L, v := NA][])
#    user  system elapsed 
#    1.00    0.87    1.25 

dt[, all.equal(v, PrevValueByGrp)]
# [1] TRUE

Todos 3 comentários

Isso não é surpreendente. Isso desaparecerá quando gforce for otimizado para := . Está na lista deste lançamento, eu acredito.

1 para este aprimoramento de desempenho. shift() é o principal gargalo em grande parte do meu código. Parece que, para um número fixo de linhas, o tempo que leva para executar shift() é proporcional ao número de grupos nos dados.

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()

screen shot 2018-11-10 at 1 08 15 pm

@ ben519 Fyi, para o caso especial de quando seu código for assim, há um atalho:

library(data.table)
dt <- data.table(Grp = rep(seq_len(1e6), each=10L))
dt[, Value := sample(100L, size = .N, replace = TRUE)]

system.time(dt[, PrevValueByGrp := shift(Value, type = "lag"), by = Grp][])
#    user  system elapsed 
#   19.50    0.80   20.34
system.time(dt[, v := shift(Value, type = "lag")][rowid(Grp)==1L, v := NA][])
#    user  system elapsed 
#    1.00    0.87    1.25 

dt[, all.equal(v, PrevValueByGrp)]
# [1] TRUE
Esta página foi útil?
0 / 5 - 0 avaliações

Questões relacionadas

jameslamb picture jameslamb  ·  3Comentários

lux5 picture lux5  ·  3Comentários

MichaelChirico picture MichaelChirico  ·  3Comentários

mattdowle picture mattdowle  ·  3Comentários

tcederquist picture tcederquist  ·  3Comentários