Using the Rcpp Timer
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Sine the 0.10.2 release, Rcpp contains an internal class Timer
which can be used for fine-grained benchmarking. Romain motivated Timer
in a post to the mailing * list where Timer
is used to measure the different components of the costs of random number generation.
A slightly modified version of that example follows below.
#include#include using namespace Rcpp; // [[Rcpp::export]] NumericVector useTimer() { int n = 1000000; // start the timer Timer timer; for(int i=0; i We get the following result, each expressing the cost per iteration in nanoseconds:
useTimer() get/put g/p+rnorm() empty loop 1.634e+03 2.573e+03 2.620e-04The interesting revelation is that repeatedly calling
GetRNGstate()
andPutRNGstate()
can amount to about 60% of the cost of RNG draws. Luckily, we usually only have to call these helper functions once per subroutine called from R (rather than repeatedly as shown here) so this is not really a permanent cost to bear when running simulations with R.It also show the usefulness of a fine-grained timer at the code level.
To leave a comment for the author, please follow the link and comment on their blog: Rcpp Gallery.
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.