[R] Slow indexing access for Matrix
Niels Richard Hansen
Niels.R.Hansen+lists at math.ku.dk
Mon Feb 23 22:09:54 CET 2009
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
More information about the R-help
mailing list