我正在尝试在Rcpp中有效地计算rowMaxs。一个非常简单的实现是
arma::mat RcppRowmaxs(arma::mat x){
int N = x.n_rows;
arma::mat rm(N,1);
for(int nn = 0; nn < N; nn++){
rm(nn) = max(x.row(nn));
}
return(rm);
}
效果很好。但是,将该功能与其他软件包进行比较,结果发现其他实现方式效率更高。具体来说,Rfast::rowMaxs
比简单的Rcpp实现快6倍以上!
[自然,我试图模仿Rfast
的行为。但是,作为Rcpp的初学者,我仅尝试按如下所述直接在Rcpp中加载Rfast::rowMaxs
。 here。不幸的是,按照我的基准测试,使用Rcpp脚本加载再次调用Rcpp脚本的R函数似乎很慢(请参见“ RfastinRcpp”行):
m = matrix(rnorm(1000*1000),1000,1000)
microbenchmark::microbenchmark(
matrixStats = matrixStats::rowMaxs(m),
Rfast = Rfast::rowMaxs(m,value=T),
Rcpp = RcppRowmaxs(m),
RfastinRcpp = RfastRcpp(m),
apply = apply(m,1,max)
)
Unit: microseconds
expr min lq mean median uq max neval cld
matrixStats 1929.570 2042.8975 2232.1980 2086.5180 2175.470 4025.923 100 a
Rfast 666.711 727.2245 842.5578 795.2215 891.443 1477.969 100 a
Rcpp 5552.216 5825.4855 6186.9850 5997.8295 6373.737 8568.878 100 b
RfastinRcpp 7495.042 7931.2480 9471.8453 8382.6350 10659.672 19968.817 100 b
apply 12281.758 15145.7495 22015.2798 17202.9730 20310.939 136844.591 100 c
关于如何提高我在上面提供的功能中的性能的任何技巧?我查看了Rfast
的源代码,并认为this是正确的文件。但是,到目前为止,我还没有找到代码的重要部分。
编辑:根据Michail的回答,现在将帖子更改为专注于Rfast
。