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

Romain Francois romain at r-enthusiasts.com
Wed Nov 21 12:48:23 CET 2012


I'll make a more general reply later (this triggered a few findings on 
the code).

Le 20/11/12 16:54, Hadley Wickham a écrit :
>> 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.
>
> I don't think they _have_to - my understanding is that the expression
> template approach is general enough that you could compile away those
> intermediates.  It may be extremely difficult to do though!
>
> Hadley

The sugar versions of e.g. pmin, ... don't make copies as the R version 
does.

For example, if x is a NumericVector :

pmin( 0.0, x )

will create an object of class : 
Pmin_Vector_Primitive<REALSXP,true,NumericVector>

Objects of that class just hold a reference to the NumericVector, and 
define an operator[](int) that when used does what you would expect and 
grabs the min value between 0.0 and x[i].

No extra memory is used.


So the idea of sugar is that we can chain these expressions and no 
additional memory is allocated for intermediate objects. There are 
though some overhead I don't quite understand yet.




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 ?


Romain

-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30

R Graph Gallery: http://gallery.r-enthusiasts.com
`- http://bit.ly/SweN1Z : SuperStorm Sandy

blog:            http://romainfrancois.blog.free.fr
|- http://bit.ly/RE6sYH : OOP with Rcpp modules
`- http://bit.ly/Thw7IK : Rcpp modules more flexible



More information about the Rcpp-devel mailing list