[R] No speed effect by using RcppArmadillo compared to R in matrix operations

Jeff Newmiller jdnewmil at dcn.davis.CA.us
Sat Oct 26 10:44:08 CEST 2013


I think you don't have accurate information about the speed of R in performing linear algebra computations. It relies on standard numerical libraries for that work, so it is as fast as those libraries are (you are unlikely to beat even an unoptimized version of those libraries with your ad hoc code). You can investigate installing custom versions of those libraries (e.g. [1]), but most performance issues arise due to inefficient handling of data during preparation or post processing.

[1] http://www.avrahamadler.com/2013/10/22/an-openblas-based-rblas-for-windows-64/
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.

Timo Schmid <timo_schmid at hotmail.com> wrote:
>Hello,
>
>I am looking for a way to do fast matrix operations (multiplication,
>Inversion) for
>large matrices (n=8000) in R. I know R is not that fast in linear
>algebra than
>other software. 
>So I wanted to write some code in C++ and incorporate this code in R. I
>have used the
>package RcppArmadillo, because a lot of people write that it is really
>fast in
>doing matrix algebra. So I have run a short example. See the code
>below.
>I was wondering that I got almost the same CPU time for the matrix
>algebra in my
>example. I expect that using C++ Code in R is faster than using the
>standard
>matrix operations in R. 
>
>Is there a way to do matrix algebra in R faster as the standard command
>(e.g. %*%) using
>the Rcpp or RcppArmadillo packages? I would be happy about any idea or
>advice.
>Thanks in advance
>
>
> > library(Rcpp)
>> library(RcppArmadillo)
>> library(inline)
>> library(RcppEigen)
>> library(devtools)
>> 
>> # Generation of the matrix
>> n=2000
>> A<-matrix(rnorm(n^2,0,1), n,n)
>> 
>> # Code in R 
>> system.time(
>+     D<-A%*%A%*%A+A)
>   user  system elapsed 
>  12.29    0.01   12.33 
>> 
>> # Code using RcppArmadillo
>> src <-
>+     '
>+ arma::mat X = Rcpp::as<arma::mat>(X_);
>+ arma::mat ans = X * X * X + X;
>+ return(wrap(ans));
>+ '
>> mprod6_inline_RcppArma <- cxxfunction(signature(X_="numeric"),
>+                                       body = src,
>plugin="RcppArmadillo")
>> 
>> system.time(
>+     C<-mprod6_inline_RcppArma(X=A))
>   user  system elapsed 
>  12.30    0.08   12.40 
>
> 		 	   		  
>	[[alternative HTML version deleted]]
>
>______________________________________________
>R-help at 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.



More information about the R-help mailing list