Speed up your R code with C++

[This article was first published on Statisfaction » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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)))

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 their blog: Statisfaction » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)