[Rcpp-devel] Checking for a promise

Dirk Eddelbuettel edd at debian.org
Sun Dec 26 21:22:34 CET 2010


On 26 December 2010 at 14:23, Gabor Grothendieck wrote:
| 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.

Yes, but -- that is the same fallacy the just-in-time people fall victim all
the time.  *If* you execute it at some point you *will* pay for it at some
point. There is (still) no free lunch.

I like the predictability of 'paying' that price ex ante.  Templates are very
strong manifestation of that approach too: spend cpu cycles at compilation
time so that you don't have to at run-time.

Anyway, this is massively off-topic.  Do play with Promises if it suits you...

| 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

That is the same type of one expression benchmark you started with last week.
I am really not interested in pathological examples.

Start with a vector or two, do some work.  I know you can write very, very
powerful and efficient vectorised expression.  I like having C++ as an
alternative via Rcpp.

Dirk

-- 
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com


More information about the Rcpp-devel mailing list