Dear all,

Indexing matrices from the Matrix package with [i,j] seems to be very slow. For 
example:

 library(rbenchmark)
 library(Matrix) 
 mm <- matrix(c(1,0,0,0,0,0,0,0), nr=20, nc=20)
 MM <- as(mm, "Matrix")
 lookup <- function(mat){
   for (i in 1:nrow(mat)){
     for (j in 1:ncol(mat)){
        mat[i,j]
     }
   }
}

 benchmark(lookup(mm), lookup(MM),  columns=c("test", "replications", 
"elapsed", "relative"), replications=50)
       test     replications elapsed relative
1 lookup(mm)           50    0.01           1
2 lookup(MM)           50    8.77      877

I would have expected a small overhead when indexing a matrix from the Matrix 
package, but this result is really surprising... 
Does anybody know if there are faster alternatives to [i,j] ?

Best regards
Søren

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to