G. B.

个人博客

且将新火试新茶,诗酒趁年华


Rcpp

Rcpp

在R中提供了C++的接口,使用Rcpp会使代码运行速度得到极大的提升,下面是一个Rcpp的小例子,它需要被保存为后缀为.cpp的文件。

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
double meanC(NumericVector x) {
  int n = x.size();
  double total = 0;

  for(int i = 0; i < n; ++i) {
    total += x[i];
  }
  return total / n;
}

// [[Rcpp::export]]
List lapply1(List input, Function f) {
  int n = input.size();
  List out(n);

  for(int i = 0; i < n; i++) {
    out[i] = f(input[i]);
  }

  return out;
}

/*** R
library(microbenchmark)
x <- runif(1e5)
microbenchmark(
  mean(x),
  meanC(x)
)
*/