[R] No speed effect by using RcppArmadillo compared to R in matrix operations
Andreas Recktenwald
a.recktenwald at mx.uni-saarland.de
Sat Oct 26 12:59:05 CEST 2013
Hi,
another option if you're using Linux AND an Intel processor would be
linking R against Intel MKL (Math Kernel Library). Under Linux you can
get a (free) non-commercial licence for it.
Here I'm using an Intel(R) Core(TM) i5-3210M CPU @ 2.50GHz laptop
processor with R 3.0.2 build with intel compilers and linked against
Intel MKL 11 and get the following times:
> set.seed(123)
> n <- 2000
> A<-matrix(rnorm(n^2,0,1), n,n)
> system.time(D<-A%*%A%*%A+A)
User System verstrichen
1.480 0.004 1.482
PS: I'm using the sequential version of Intel MKL.
Zitat von Timo Schmid <timo_schmid at hotmail.com>:
> 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