Speed up your R code with C++

February 3, 2011
By

(This article was first published on Statisfaction » R, and kindly contributed to R-bloggers)

Hi again,

Recently, Julyan blogged about allocating memory first before modifying R objects. That’s a very useful trick that applies to most programming languages. Tonight I want to blog about something a bit more complicated and more specific to R, but which can lead to massive speed-ups: interfacing C/C++ within R codes.

The rationale behind is that R is an amazing programming language for statisticians, with great packages available to do pretty much everything, from data analysis to beautiful graphs… but it runs definitely slower than low-level or middle-level languages like C or  C++. No worries, it’s pretty easy to insert C++ snippets inside R programs, using packages like inline and Rcpp. For those of you who already use python, it’s very similar to scipy.weave. Here’s an example:

library(inline)
doublematrix <- cxxfunction(signature(x = "numeric"), body = '
Rcpp::NumericMatrix xcpp(x);
int nrows = xcpp.nrow();
int ncolumns = xcpp.ncol();
for (int i = 0; i < nrows; i++){
    for (int j = 0; j < ncolumns; j++){
        xcpp[nrows * j + i] *= 2;
    }
}
return xcpp;
', plugin="Rcpp")
print(doublematrix(matrix(1:10, nrow = 2)))

Created by Pretty R at inside-R.org

This code defines a function “doublematrix” that takes a matrix x as an argument, and doubles all of its elements. As you can see the C code is inside a string called “body”. Hence, you don’t even have to handle separate files, the package takes care of all the boring stuff! To run this example on your machine, you just have to install inline and Rcpp, which are both on CRAN.

Note that the Rcpp “plugin” allows to use convenient types within the C code (“Rcpp::NumericMatrix” for instance), the type conversion is done implicitly, you don’t have to deal with the garbage collector… it’s already good enough for me! And much more advanced functionalities are supported (C++ templates, types equivalent to R data frames and many other types, though I didn’t find the simple equivalent of “array”, easy links with STL and GSL functions…). Have a look at the Rcpp webpage if you want to know more, especially the FAQ!

Note that the adaptive MCMC package developed by Jeff Rosenthal provides a good start to look into more advanced interfacing techniques, where pre-compiled C functions are loaded dynamically within R.


To leave a comment for the author, please follow the link and comment on his blog: Statisfaction » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: ,

Comments are closed.