Rcpp rowMaxs与matrixStats rowMaxs

问题描述 投票:3回答:1

我正在尝试在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::rowMaxshere。不幸的是,按照我的基准测试,使用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

r rcpp
1个回答
2
投票

我刚刚在笔记本电脑上做了一些实验。我有5岁的惠普,带有2个intel i5内核,频率为2.3 GHz。附上一张我的成绩照片。 Rfast的实现总是比matrixStats的实现快得多,而且随着矩阵变大,时间差也会增加。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.