[Rcpp-devel] R vectorisation vs. C++ vectorisation
Darren Cook
darren at dcook.org
Tue Nov 20 00:38:01 CET 2012
> The example is not particularly well chosen, but I think the problem
> of vectorisation is a real one. To vectorise code in R you need to
> have a big R vocabulary; to vectorise code in Rcpp, you need to be
> able to write a loop. So even if it's a not a completely fair
> comparison to R, it's still reasonable because it's much easy to
> vectorise in C++.
In my case, informed by interesting benchmarks such as yours, I now tend
to write my R functions with optimizing to C++ in mind. In other words,
I've stopped being ashamed of having loops in my R code.
If my R script completes Quick Enough I don't care. But when it takes
Too Long, I know I can improve it by one order of magnitude by writing
better R code, or by two orders of magnitude by porting just the
bottleneck to C++, using Rcpp and inline. For me, at least, those two
choices are about equal effort.
Darren
P.S. I don't think the sugar versions can be made any quicker, because
they have to allocate intermediate vectors, and do more memory copies.
>> | exploration of vectorisation in R vs C++ at
>> | https://gist.github.com/4111256
>> | ...
>> | expr min lq median uq max neval
>> | vacc1(age, female, ily) 6816.8 7139.4 7285.7 7823.9 10055.5 100
>> | vacc2(age, female, ily) 194.5 202.6 212.6 227.9 260.4 100
>> | vacc3(age, female, ily) 21.8 22.4 23.4 24.9 35.5 100
>> | vacc4(age, female, ily) 36.2 38.7 41.3 44.5 55.6 100
>> | vacc5(age, female, ily) 29.3 31.3 34.0 36.4 52.1 100
More information about the Rcpp-devel
mailing list