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

Wray, Christopher christopher.wray.10 at ucl.ac.uk
Thu Aug 18 16:30:42 CEST 2011


Thanks for responses, sure I do make use of the R side - when required and where there is not a significant penalty.

I suppose the root of my question (aside from the functions calls complicating and slowing things down) was that if in R, I might do something like Vector1[subvector_of_relevant_indices] = Vector2, say:

R> (1:50)[runif(10,0,20)]

And I was wondering if I had missed some sugar-type of construct that would allow me to avoid the explicit 1e7 loop - and instead make use of some Rcpp vectorised/optimised/ machinery. Christian's sugg' of allowing double->int did speed things up enough, by me to chuck away the "floor"...

Aside from that, the genral theme I was aiming for was "sampling via indices" which I assumed someone may had already have a wheel which they had invented..already.. :-)

No probs - and sorry for the unclear post! Thanks

________________________________________
From: rcpp-devel-bounces at r-forge.wu-wien.ac.at [rcpp-devel-bounces at r-forge.wu-wien.ac.at] on behalf of Dirk Eddelbuettel [edd at debian.org]
Sent: 18 August 2011 14:39
To: xian at unm.edu
Cc: rcpp-devel at r-forge.wu-wien.ac.at
Subject: Re: [Rcpp-devel] R.e. Speed gain assistance (Wray, Christopher)

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
_______________________________________________
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


More information about the Rcpp-devel mailing list