Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

So I’ve been curious about a different kind of performance comparison between Rcpp11 and Rcpp, i.e. I’ve benchmarked the time it takes to compile the following code (the example you get from RStudio when you do new C++ file) with Rcpp and Rcpp11.

#include
using namespace Rcpp;

// [[Rcpp::export]]
int timesTwo(int x) {
return x * 2;
}


We’ll compare Rcpp::sourceCpp and attributes::sourceCpp. They both generate essentially the same decoration code.

require(microbenchmark)
compile_Rcpp <- function(){
Rcpp::sourceCpp("/tmp/timesTwo.cpp", rebuild = TRUE )
}
compile_Rcpp11 <- function(){
attributes::sourceCpp( "/tmp/timesTwo.cpp" )
}


These timings come from my iMac running Mavericks.

> microbenchmark( Rcpp = compile_Rcpp(), Rcpp11 = compile_Rcpp11(), times = 10 )
Unit: seconds
expr      min       lq   median       uq      max neval
Rcpp 3.061308 3.068119 3.069977 3.071560 3.258582    10
Rcpp11 1.475950 1.479628 1.480901 1.485374 1.512100    10


This is interesting. Twice as fast, although this has not been a goal, this is still nice to have, nobody likes waiting for the compiler more than needed. I'll probably produce more benchmarks with various code examples and various compilers for the article.

Rcpp11 is much smaller in terms of size of the code base (the number of lines in the various header files), and I think these benchmark show us the consequence of that. Less code to #include yields better performance. Sweet.