[Rcpp-devel] R vectorisation vs. C++ vectorisation

Hadley Wickham h.wickham at gmail.com
Mon Nov 19 17:05:27 CET 2012

> Unsurprisingly the R loop (vacc1) is very slow, and proper
> vectorisation speeds it up immensely.  Interestingly, however, the C++
> loop still does considerably better (about 10x faster) - I'm not sure
> exactly why this is the case, but I suspect it may be because it
> avoids the many intermediate vectors that R requires.

A little bit of exploration on that theme: https://gist.github.com/4111432

The R versions incur a cost of about 100µs for each additional
intermediate vector, whereas the C++ cost is much smaller. Again, most
of the time this isn't going to matter, but if you are trying to eke
out every possible iota of speed, then rewriting already vectorised
code in C++ may give significant speed increases if you have many
intermediate values.


RStudio / Rice University

More information about the Rcpp-devel mailing list