Christian Gunning has a great example of using Rcpp to speed up a for loop in R. For his agent-based simulation, Christian needed to repeatedly call the rbinom function in a loop. (Unfortunately, you can't pass a vector to the size argument, which would have solved the problem.) Using the aaply function (from the plyr package) took about 38 sections for 10,000 simulations: aaply gives you nice concise code, but it's bit like using a hammer to crack a walnut in this case. An explicit for loop took just over a second. But rewriting the body of the loop in C++ (but still calling R's native binomial RNG, via the standard R API) reduced the time by a factor of over 50 compared to the for loop: down to 0.021 seconds. cxxfunction in the inline package makes it super simple to incorporate C++ code into your R loops (provided you know C++ of course) -- see Christian's full post at the link below to see how it's done.
Life in Code: Efficient loops in R -- the complexity versus speed trade-off
Wow, this is great!
Posted by: Clio | June 23, 2011 at 15:36
Christian also mentioned this on the Rcpp-devel mailing list and I replied with a collection of alternatives. If you want to stay in pure R code you can use sapply or, even better, vapply to out-perform the loop. When using Rcpp it is worthwhile employing the STL algorithms when possible and I posted a version doing so.
The wonderful thing about Rcpp is that it is very close to programming in R at times and definitely much easier than programming in C with the R API. In particular all the PROTECT/UNPROTECT gymnastics are taken care of by the constructors and destructors of objects.
I sent David a copy of my posting by email.
Posted by: Douglas Bates | June 23, 2011 at 17:24
Thanks for the comments, Doug. To everyone else, you can find Doug's alternatives in the Rcpp-devel archives.
Posted by: David Smith | June 24, 2011 at 10:59