Dirk, good point. This is a very simple function and if I add complexity at the end there's no significant difference between calling -fun-, -cppFuncall- and -RcppFuncall-. Just to make this thread more complete, if I modify -fun- from
fun <- function(x) { -cos(x[1])*cos(x[2])*exp(-((x[1] - pi)^2 + (x[2] - pi)^2)) } to fun <- function(x) { w<-sapply(1:1e3,function(x) -cos(x[1])*cos(x[2])*exp(-((x[1] - pi)^2 + (x[2] - pi)^2))) 1 } Running the benchmark I get Unit: relative expr min lq mean median uq max neval cppFuncall(x, fun) 1 1 1.044215 1 1 6.1 1000 RcppFuncall(x, fun) 1 1 1.018957 1 1 2.7 1000 fun(x) 1 1 1.000000 1 1 1.0 1000 Which is way more reasonable from what I was getting at first. No significant difference overall. Kevin, thanks for the example, now I get why isn't a good idea! This has been very useful to me. Best, George G. Vega Yon +1 (626) 381 8171 http://www.its.caltech.edu/~gvegayon/ On Wed, Aug 3, 2016 at 12:34 PM, Kevin Ushey <kevinus...@gmail.com> wrote: > The simplest demonstrating example I can think of: > > --- > > #include <Rcpp.h> > using namespace Rcpp; > > struct A { ~A() { Rprintf("~A()"); } }; > > // [[Rcpp::export]] > void ouch() { > A a; > Rf_error("ouch!"); > } > > /*** R > ouch() > */ > > --- > > Call 'Rcpp::sourceCpp()' on that and you'll see: > > > Rcpp::sourceCpp('~/Desktop/Untitled.cpp') > > ouch() > Error in ouch() : ouch! > > Note that the destructor was not called. Replace `Rf_error` with > `Rcpp::stop` and you will see the destructor is called. > > It's possible that you won't have a memory leak per-se (if the memory > is all allocated on the stack, maybe the runtime still knows to just > clear the entire stack after something like this) but not running > destructors is definitely a big problem. > > Cheers, > Kevin > > On Wed, Aug 3, 2016 at 12:22 PM, Dirk Eddelbuettel <e...@debian.org> wrote: > > > > On 3 August 2016 at 11:38, George Vega Yon wrote: > > | Thanks for the quick reply! What kind of errors are we talking about? > I a new > > | run I explicitly caused an error by passing a character vector, and > had no > > | memory leak (using Valgrind): > > | > > | cppFuncall(letters, fun) > > | Error in cos(x[1]) : non-numeric argument to mathematical function > > | > > | If its not too much to ask, could you give an explicit example in > which that > > | happens (memory leak)? Just trying to learn here! > > > > You are misreading what Kevin said. You short ten-line example runs > fine. > > We are not saying it has an error. > > > > What Kevin explained to you is that in the context of larger programs, > > possibly with inputs you don't know yet, some errors may occur. And both > > Rcpp:Function() and Rcpp::stop() can recover from that. > > > > Your example cannot. So by all means use it as a small (local) script > if the > > few milliseconds matter to you. But think twice about using it in a > larger > > context, or about promoting it as a general solution, and understand why > we > > can't put it into Rcpp as is. > > > > Hth, Dirk > > > > -- > > http://dirk.eddelbuettel.com | @eddelbuettel | e...@debian.org >
_______________________________________________ Rcpp-devel mailing list Rcpp-devel@lists.r-forge.r-project.org https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel