Consider the following little "benchmark"

> require(Matrix)
> tmp <- Matrix(c(rep(1,1000),rep(0,9000)),ncol=1)
> ind <- sample(1:10000,10000)
> system.time(tmp[ind,])
   user  system elapsed
  0.004   0.001   0.005

> ind <- sample(1:1000,10000,replace=TRUE)
> system.time(tmp[ind,])
   user  system elapsed
  0.654   0.006   0.703

> system.time(Matrix(as(tmp,"matrix")[ind,]))
   user  system elapsed
  0.005   0.000   0.006

First I access all 10000 rows in a random order, which is fast,
but when I access the first 1000 rows 10000 times there is a
considerable slowdown. Last I convert back and forth
between matrix and Matrix and get a serious speedup. Am I missing
a point here? Should I not use indexing with "[" for the
sparse matrices if I have repeated indices?

I'm running Mac OS X, version 10.5.6, with Matrix package
version 0.999375-21.

I hope that somebody can enlighten me on this issue.

Thanks, Niels


--
Niels Richard Hansen
Associate Professor
Department of Mathematical Sciences
University of Copenhagen
Universitetsparken 5
2100 Copenhagen Ø
Denmark
+45 353 20783

______________________________________________
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