使用Rcpp优化结构

时间:2019-02-26 10:04:33

标签: r optimization rcpp

我正在尝试加快strucchange软件包(source)中的breakpoints.formula函数。它在extend.RSS.table中有一个瓶颈。 这是我尝试使用Rcpp重新布线其中的for(i in my.index)循环的方法:

// [[Rcpp::export]]
NumericMatrix fillMyRSStable(NumericMatrix myRSStable, List & RSStriang, IntegerVector & myIndex, int m, float h) {

  NumericMatrix myRSStableCPY = clone(myRSStable);
  CharacterVector indexI(1), indexj(1);
  int n = myIndex.size();
  CharacterVector nms = rownames(myRSStableCPY);

  for(int myIndexI = 0; myIndexI < n; ++myIndexI) {
    int i = myIndex[myIndexI];

    IntegerVector potIndex =  seq((m - 1)*h, i - h);
    //sapply
    int np = potIndex.size();
    NumericVector breakRSS(np);

    for(int pin = 0; pin < np; ++pin) {
      int j = potIndex[pin];
      NumericVector RSStriangI = as<NumericVector>(RSStriang[j]);

      indexj[0] = j;
      IntegerVector matchedIdxJ = match(indexj, nms);

      breakRSS[pin] = myRSStableCPY(matchedIdxJ[0] - 1, 1) + RSStriangI[i - j - 1];
    }
    // end sapply
    int opt = which_min(breakRSS);

    indexI[0] = i;
    IntegerVector matchedIdxI = match(indexI, nms);

    myRSStableCPY(matchedIdxI[0] - 1, 2) = potIndex[opt];
    myRSStableCPY(matchedIdxI[0] - 1, 3) = breakRSS[opt];
  }
  return myRSStableCPY;
}

此代码比R快一点,但没有我想象的那么快。有人可以发现下面的代码有任何明显的低效之处吗?任何一般性(或特定性)建议都会有所帮助。

编辑分析代码:

library(strucchange)
library(profvis)
library(microbenchmark)
data("Grossarl")
pr <- profvis(microbenchmark(Grossarl.bp <- breakpoints(fraction ~ 1, data = Grossarl, h = 0.1)))
htmlwidgets::saveWidget(pr, "profile.html")

链接到我的Rcpp版本:https://github.com/s-Nick-s/strucchange

0 个答案:

没有答案