[Rcpp-devel] R vectorisation vs. C++ vectorisation
darren at dcook.org
Wed Nov 21 13:22:42 CET 2012
>>> 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.
By "sugar versions" I meant vacc4() vs. vacc3()
(https://gist.github.com/4111256) not pmin() and friends. The vacc4()
code looks like:
p = 0.25 + 0.3 * 1 / (1 - exp(0.04 * age)) + 0.1 * ily;
p = p * ifelse(female, 1.25, 0.75);
p = pmax(0,p);
p = pmin(1,p);
Each operation copies the whole NumericVector each time, each of which
needs a memory allocation.
vacc3a() does the same pipeline of operations on a single double, which
is therefore likely to be a single CPU register, and the whole of
vacc3a() will be inlined in vacc3().
> Also, I'm probably goinf to add a function that does the equivalent of :
> pmax( b, pmin( a, x ) )
> I'm thinking of naming it pminmax. Any better idea ?
I'd expect a function called minmax() to return two values, the min and
max function. The "p" prefix is too subtle for me :-)
How about clip():
p = clip(p, 0.0, 1.0);
This is such a common operation that I'm surprised R doesn't already
have a function like this!
Darren Cook, Software Researcher/Developer
http://dcook.org/work/ (About me and my work)
http://dcook.org/blogs.html (My blogs and articles)
More information about the Rcpp-devel