问题描述
观察
对于中等大小的矩阵,对于arma::mat
类型,将矩阵从R传递到C ++的开销要比NumericMatrix
类型慢得多.就像拍摄250倍左右的镜头一样.这是一个最小的例子
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 ;
}
然后,在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
因此,仅将矩阵作为arma::mat
类型传递比NumericMatrix
慢约250倍.太疯狂了!所以...
So just passing a matrix as an arma::mat
type is around 250x slower than NumericMatrix
. That's crazy! So...
提出的问题
- 这是怎么回事?为什么
mat
so 比NumericMatrix
慢得多? - 有没有解决这个问题的好方法?我遇到一个问题,我需要在一个被调用很多次的函数中为一些非常简单的矩阵代数使用
arma::mat
.我目前在整个过程中都使用arma
类型,并且我的代码比我预期的要慢很多(这就是我最终编写上述愚蠢示例的方式). 250x的速度损失是如此之大,以至于我要重写大部分代码以在整个过程中使用NumericMatrix
类型.实际上,我可能最终会为NumericMatrix
和完全放弃arma
类型编写自己的矩阵乘法函数.但是在我这样做之前,有没有更好的解决方案?
- 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?
(尽管我猜想另一种读法不是arma::mat
从R类型转换很慢,而是NumericMatrix
类型非常有效!)
(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!)
推荐答案
我相信这会创建一个新的犰狳矩阵,然后复制您的数字矩阵的内容.
I believe this creates a new Armadillo matrix then copies the contents of your numeric matrix.
要强制将NumericMatrix转换为arma :: mat类型,应使用以下代码:
To cast the NumericMatrix to type arma::mat, you should use the following:
// [[Rcpp::export]]
double test_const_arma( const mat& X ) {
return 0.0 ;
}
我的机器上的速度比较:
Speed comparison on my machine:
microbenchmark( test_const_arma( XX ), test_nm( XX ), test_arma( XX ), test_nm_conv( XX ))
## Unit: microseconds
## expr min lq mean median uq max neval
## test_const_arma(XX) 1.852 2.381 3.69014 2.7885 4.3490 11.994 100
## test_nm(XX) 1.925 2.455 3.47679 2.8535 3.5195 21.222 100
## test_arma(XX) 68.593 71.212 83.63055 73.4555 98.8070 278.981 100
## test_nm_conv(XX) 68.700 70.983 80.55983 73.1705 82.2665 183.484 100
这篇关于R矩阵到犰狳的转换真的很慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!