[Rcpp-devel] R.e. Speed gain assistance (Wray, Christopher)

Dirk Eddelbuettel edd at debian.org
Thu Aug 18 15:39:50 CEST 2011


On 18 August 2011 at 01:14, Christian Gunning wrote:
| There's a number of things going on in your example.  Are you sure
| that sample() is the bottleneck?  You might want to try breaking this
| into smaller pieces and benchmarking each with rbenchmark.

I had the same initial thought. There was simply too much going on.
 
| On Wed, Aug 17, 2011 at 3:00 AM,
| <rcpp-devel-request at r-forge.wu-wien.ac.at> wrote:
| >
| > I have thought (but not tried) importing R's sample function...but I suspect this may be a totally rude idea.
| 
| Pulling R functions into a C++ function isn't hard, but there's
| overhead -- if i recall correctly, it's appreciably slower than the
| API.

Yup. And again: you can measure this.

I see direct calls of R functions as a convenient stop-gap measure for
something you may have to do once in a while. Not a million times in a big
bootstrapping exercise.
 
Dirk

| Take a look at this (unweighted) sample() function.  It's giving R a
| run for it's money, and is pretty fast even for very large n, and it
| looks statistically correct (not sure if I'm glossing over ugly,
| machine-specific details of double->int conversion here). Does this
| shed any light on your question?
| 
| best,
| Christian
| 
| library(inline); library(Rcpp);
| src1<-'
| NumericVector xx(x);
| int xx_sz = xx.size()-1; // index of last xx
| int nn=as<int>(n);
| NumericVector ret(nn);
| RNGScope scope;
| for (int i=0; i<nn; i++) {
|     ret[i] = xx[Rf_runif(0.0,xx_sz)];
| };
| return(ret);
| '
| 
| mysample<-cxxfunction( signature(x='numeric', n="numeric"), src1, plugin='Rcpp')
| 
| system.time(result <- mysample(1:50, 1e7))
| system.time(resultR <- sample(1:50, 1e7, replace=T))
| 
| 
| -- 
| A man, a plan, a cat, a ham, a yak, a yam, a hat, a canal – Panama!
| _______________________________________________
| 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

-- 
Two new Rcpp master classes for R and C++ integration scheduled for 
New York (Sep 24) and San Francisco (Oct 8), more details are at
http://dirk.eddelbuettel.com/blog/2011/08/04#rcpp_classes_2011-09_and_2011-10
http://www.revolutionanalytics.com/products/training/public/rcpp-master-class.php


More information about the Rcpp-devel mailing list