An Observation
For medium-size matrices, the overheads on passing matrices from R to C++ are massively slower for arma::mat
types than for NumericMatrix
types. Like taking around 250x as long. Here's a minimal example
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
using namespace Rcpp;
using namespace arma;
// [[Rcpp::export]]
double test_nm( NumericMatrix X ) {
return 0.0 ;
}
// [[Rcpp::export]]
double test_arma( mat X ) {
return 0.0 ;
}
// [[Rcpp::export]]
double test_nm_conv( NumericMatrix X ) {
mat X_arma = as<mat>( X ) ;
return 0.0 ;
}
Then, in R:
XX <- matrix( runif( 10000 ), 2000, 50 )
microbenchmark( test_nm( XX ), test_arma( XX ), ( XX ) )
Unit: microseconds
expr min lq mean median uq max neval
test_nm(XX) 5.541 16.154 16.0781 17.577 18.876 48.024 100
test_arma(XX) 1280.946 1337.706 1404.0824 1361.237 1389.476 3385.868 100
test_nm_conv(XX) 1277.417 1338.835 1393.4888 1358.128 1386.101 4355.533 100
So just passing a matrix as an arma::mat
type is around 250x slower than NumericMatrix
. That's crazy! So...
Questions arising
- What's going on? Why is
mat
so much slower thanNumericMatrix
? - Is there a good way to deal with this? I've got a problem where I need to use an
arma::mat
for some fairly simple matrix algebra in a function that gets called a lot of times. I'm currently usingarma
types throughout, and my code is much slower than I expected (that's how I ended up cooking up the dumb examples above). A speed penalty of 250x is such a big deal that I'm about to rewrite large sections of code to useNumericMatrix
types throughout. In fact, I might end up writing my own matrix multiplication function forNumericMatrix
and abandonarma
types altogether. But before I do, are there any better solutions?
(Although I guess another way to read this is not that arma::mat
is slow to convert from R types, but that the NumericMatrix
type is amazingly efficient!)