The speed of dense or sparse matrix algebra

I will work in R with a rather large (7 e6 x 4.5 e3), but very sparse matrix. Therefore, I am trying to understand how to work effectively with sparse matrices. I have two related questions.

First: I was given to understand that the package is Matrixautomatically linked to the compiled DLLs LAPACK and SuiteSparse. (I work on Windows.) It seemed to me that using SuiteSparse routines would reduce execution time compared to using dense matrices using the LAPACK package. But the test run below shows that the runtime for a sparse version of the matrix is ​​much slower than the dense version.

> library(Matrix)
> sparse <- sparseMatrix(1:4, 1:4, x=rnorm(4))
> dense <- as.matrix(sparse)
> x <- 1:4
> system.time(for (i in 1:10000) sparse %*% x)
   user  system elapsed 
   0.23    0.00    0.23 
> system.time(for (i in 1:10000) dense %*% x)
   user  system elapsed 
      0       0       0 
> system.time(for (i in 1:1000) solve(sparse))
   user  system elapsed 
   3.94    0.00    3.94 
> system.time(for (i in 1:1000) solve(dense))
   user  system elapsed 
   0.05    0.00    0.05

a) , Matrix ? , DLL? ) , ?

-: RcppEigen RcppArmadillo. RcppArmadillo ( ). RcppEigen, , . - , Eddelbuettel Sanderson, RcppEigen?

+4
1

( .) ; , , (, 25% ). ( 1000x1000) 26 , . , Matrix , (Rcpp)Eigen/(Rcpp)Armadillo...

library(rbenchmark)
library(Matrix)
set.seed(101)
sparse <- sparseMatrix(1:1000,1:1000,x=rnorm(1000))
dense <- as.matrix(sparse)
benchmark(solve(sparse),solve(dense),replications=20,
          columns = c(
       "test", "replications", "elapsed", "relative", "user.self"))
##            test replications elapsed relative user.self
## 2  solve(dense)           20   6.932   26.868     6.692
## 1 solve(sparse)           20   0.258    1.000     0.256
+7

All Articles