Daily news about using open source R for big data analysis, predictive modeling, data science, and visualization since 2008

August 22, 2012

Benchmarking random-number generation from C++

If you're writing C++ code and want to generate random numbers, you might not be aware that R provides an API to call the R RNG functionality directly. The Rcpp package's "syntactic sugar" feature makes this process easier, by automating the process of translating a subset of ordinary R code into compiled C++ code. That means you can write code that looks like R in C++:

draws = rnorm(N_, 0.0, 1.0) ;

and have it compiled to directly into C++ code, using R APIs where appropriate. You can even write R for loops and have them converted to compiled C++ loops. That prompted R user Jonathan Olmsted to benchmark four ways of generating a million random numbers (results from fastest to slowest):

Interestingly, 3 vs 4 is a case where calling the vectorized function in R directly is faster than writing a loop in C++, but the new vectorized API in Rcpp is the fastest option by far. Jonathan's code (linked below) is also a great example of using the benchmark function (from the rbenchmark package) to compare performance of snippets of R code.