Site icon R-bloggers

Benchmarking random-number generation from C++

[This article was first published on Revolutions, 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.

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

  1. Compiled vectorized call to rnorm (about 1.6 seconds)
  2. Compiled loop calling Rf_rnorm (an older Rcpp API to the RNG) (about 50% slower)
  3. Vectorized call to rnorm in R (not using C++, also about 50% slower)
  4. Compiled loop calling rnorm (about over 250% slower)

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.

Jonathan P. Olmsted Blog: RNG Performance with Rcpp

To leave a comment for the author, please follow the link and comment on their blog: Revolutions.

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.