Search code examples
rsimulationrcppsampling

Most efficient way to run many repetitions of an Rcpp function for a simulation study?


I wrote an Rcpp function that returns a sample from a population, and to test the estimation method I'd like to run it thousands or millions of times. It seems that invoking Rcpp takes a little bit of overhead, and something like replicate(100,myFunction()) is taking much longer than I would expect.

What's the best way to do this? rep,*apply, replicate, put the loop itself in C++?


Solution

  • If you are concerned about performance you want to minimize the number of calls from R to C++ -- and not do it thousands or millions of times as you said.

    So Baptiste is spot on: do the for() loop at the C++ level, if you can.

    Also note that the most recent version of RcppArmadillo now has a C++ version of sample() which may allow you to do that part in C++ too. That saidm I know nothing about "UPtille" and what it does so maybe you are stuck with the existing R implementation.