[Rcpp-devel] Checking for a promise

Gabor Grothendieck ggrothendieck at gmail.com
Sun Dec 26 20:23:17 CET 2010


On Sun, Dec 26, 2010 at 1:55 PM, Dirk Eddelbuettel <edd at debian.org> wrote:
>
> On 26 December 2010 at 13:36, Gabor Grothendieck wrote:
> | On Sun, Dec 26, 2010 at 12:07 PM, Dirk Eddelbuettel <edd at debian.org> wrote:
> | >
> | > On 25 December 2010 at 17:22, Gabor Grothendieck wrote:
> | > | It seems that there are no facilities for this in Rcpp -- at least I
> | > | haven't found them.  It seems one has to go one level lower to do
> | >
> | > Patches are always welcome, particularly if they come with use cases
> | > motivating them ("Does it make things easier? Or faster? Or at least make
> | > them possible?")  as well as unit tests and documentation.
> | >
> | > | this.  The code below works.  It passes an environment and name and
> | > | returns the type code.  I think this sort of functionality was
> | > | intentionally left out of R itself and if one were just trying to
> | > | emulate R then I can understand it not being there but since Rcpp is
> | > | intended for performance it might be a good idea to give this sort of
> | > | access to promises without forcing the program to use the lower level
> | > | facilities.
> | >
> | > Well, why?  I do not use delayedAssign() all that much in R; I haven't needed
> |
> | I did address this already in the paragraph you quote but in case it
> | wasn't clear its to make the code run faster.  Promises that are not
> | used never need be evaluated saving the time that such evaluation
> | would have otherwise taken.
>
> Why would I pass an expression I never expect to be evaluated?
>

Because you don't know ahead of time whether it will be used or not.
Here is an example that one can speed up by a factor of 500x by using
R instead of Rcpp. I guess you could say its .Call's fault but the
fact remains that R will be much faster than Rcpp whenever this comes
into play.

> library(Rcpp)
> library(inline)
> library(rbenchmark)
>
> f1 <- function(which, x, y) if (which == 1) x else y
> f2 <- cxxfunction(sig = c(which = "numeric", x = "SEXP", y = "SEXP"),
+ body = '
+ SEXP z;
+ int w = as<int>(which);
+ if (w == 1) z = x; else z = y; return z;
+ ', plugin = "Rcpp")
>
> benchmark(replications = 1000,
+ R = f1(1, 10, rnorm(10000)),
+ Rcpp = f2(1, 10, rnorm(10000))
+ )
  test replications elapsed relative user.self sys.self user.child sys.child
1    R         1000    0.01        1      0.01        0         NA        NA
2 Rcpp         1000    4.82      482      4.79        0         NA        NA


-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com


More information about the Rcpp-devel mailing list