Benchmarking random-number generation from C++

August 22, 2012

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

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. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, 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.


Mango solutions

RStudio homepage

Zero Inflated Models and Generalized Linear Mixed Models with R

Dommino data lab

Quantide: statistical consulting and training



CRC R books series

Six Sigma Online Training

Contact us if you wish to help support R-bloggers, and place your banner here.

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)