Make `[[.indedex_array faster` #27

Closed
hadley opened this Issue Feb 26, 2011 · 0 comments

Comments

Projects
None yet
1 participant
@hadley
Owner

hadley commented Feb 26, 2011

Compare splitter_a to split:

df <- data.frame(x = sample(100, 1e3, rep = T))
system.time(x <- as.list(splitter_a(df, 1)))
system.time(y <- split(df, 1:nrow(df)))
all.equal(x, y)

Should be able to speed up by specialising for common cases

"[[.indexed_array" <- function(x, i) {

  if (ncol(x$index) == 2) {
    x1 <- x$index[[1]]
    x2 <- x$index[[2]]
    if (x$subs[1] == "[") {
      return(x$env$data[x1[i], x1[2], drop = x$drop])
    }
    if (x$subs[2] == "[[") {
      return(x$env$data[[x1[i], x1[2], drop = x$drop]])
    }
  }

  indices <- x$index[i, ,drop=TRUE]
  call <- paste("x$env$data", x$subs[1], paste(indices, collapse = ","), ",
    drop = ", x$drop, x$subs[2], sep = "")
  eval(parse(text = call))
}

@hadley hadley closed this in b34e78b Oct 6, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment