[Rcpp-devel] Using pointers with Numeric Vectors

Dirk Eddelbuettel edd at debian.org
Thu Dec 20 20:29:25 CET 2012


On 20 December 2012 at 13:25, Alon Honig wrote:
| Thank you for providing a point of reference with regards to speed. Dirk's
| examples have ratios of 60:1 causing me to rethink my approach.

That is __obviously__ problem dependent. 

We have seen anything from more than 10k (for somewhat degenerate cases and
our sugar "quit quickly" approach; see the Rcpp-introduction and paper) to
several hundred (recursive function calls in Fibonacci) to these 60-some for
looping examples (MCMCC Gibbs, VAR(2) model, ...) to just two or less
(previous email).

What you compare against matters.

Very sage advice by Hoa below.

Dirk
 
| On Thu, Dec 20, 2012 at 1:18 PM, H Xiong <haoxiong.ucsf at gmail.com> wrote:
| 
|     Hi Alon,
| 
|     I am only a user, not a developer, of Rcpp, so I will offer some
|     observations only.
| 
|     On Thu, Dec 20, 2012 at 9:35 AM, Alon Honig <honeyoak at gmail.com> wrote:
| 
|         My current RCPP program is only 4 times faster than its R byte code
|         compiled equivalent. I am trying to speed up my code and from what I
|         understand using pointers prevents an external function from copying
|         the entire object when executing its methods.   I am having difficulty
|         finding salient examples that use points and NumericVectors. Please
|         tell me how in this same code I could get the functions "get_sum" and
|         "get_var" to point to the NumericVector object "v" rather than copy the
|         whole thing.
| 
| 
|     Many of R commands have C or Fortran implementation so it is not always the
|     case that Rcpp program will be many times faster. I once translated an R
|     loop into C and I roughly doubled the speed, so 4 times speedup isn't
|     bad. Your examples are very simple functions that R  has built-in commands
|     for and it is unlikely you can surpass R implementation in overall
|     robustness and numeric stability.
| 
|     I don't recommend messing with raw pointers. Even if you are familiar with
|     C interface of R's garbage collector, it is better to let Rcpp take care of
|     the low level details for you. You can use constant reference to pass
|     objects around and there is no copy involved (see below). You just need to
|     be careful that objects are held by some variables and not garbage
|     collected by R's garbage collector. 
|      
| 
| 
| 
|             library(inline)
|             library(Rcpp)
|              a=1:1000000
|             Rcpp.var = cxxfunction(signature(input="numeric"), plugin="Rcpp",
|             body="
| 
|                  NumericVector v = input; 
| 
|     Here there is no copy, because the constructor for NumericVector taking
|     SEXP knows how to  use pointers. Note that this no-copy behavior only
|     applies when SEXP is holding an object of the same type as the
|     constructor's type.
| 
| 
|                  int n = v.size();
| 
|                  double v_mean =  get_sum(v,n)/n;
| 
|                  double v_var = get_var(v,v_mean,n);
| 
|                  return wrap(v_var);
| 
| 
|                 ",includes="
| 
| 
|                  double get_var(NumericVector v,double m,int l)
| 
|     double get_var(const NumericVector& v, double m, int l) 
| 
| 
|                  {double a = 0;
| 
|                      for (int i = 0; i <l;i++)
| 
|                 {a += (v[i]-m)*(v[i]-m);}
| 
|                 return(a/l);
| 
|                  }
| 
| 
| 
|                  double get_sum(NumericVector v,int l)
| 
|     double get_sum(const NumericVector& v, int l) 
| 
|     Hao
| 
| 
|                  { double s = 0;
| 
|                    for (int i = 0; i <l;i++)
| 
|                 {s += v[i];}
| 
|                 return(s);
| 
|                  }
| 
|              ")
| 
|              b=system.time(for (i in 1:100)Rcpp.var (a))
|              c= system.time(for (i in 1:100)var (a))
| 
| 
| 
|         Thank you Alon.
| 
|         P.S. I am aware that the "get_var" function provides the population
|         variance and not the sample variance.
| 
| 
|         _______________________________________________
|         Rcpp-devel mailing list
|         Rcpp-devel at lists.r-forge.r-project.org
|         https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
| 
| 
| 
| 
| 
| ----------------------------------------------------------------------
| _______________________________________________
| Rcpp-devel mailing list
| Rcpp-devel at lists.r-forge.r-project.org
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
-- 
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com  


More information about the Rcpp-devel mailing list