On 29/05/2012 09:57, Alon Wasserman wrote:
Hi,
We've encountered a difference in running time between a straight function
call and the same call using do.call when the called function generated an
error. We've isolated the problem to the following small reproducible
example:

Consider the following function:
foo<- function(nr = 2e6, nc=3, use.do.call = FALSE) {
   nn<- paste("V", 1:nc, sep="")
   z<- data.frame(matrix(rnorm(nr*nc), nrow=nr, ncol = nc, dimnames =
list(NULL, nn)))

   foo2<- function(x) x[,"V1"] + x[,"V0"]
   if (use.do.call)
     do.call(foo2, list(z))
   else
     foo2(z)
}

foo2, when called, generates an error because it accesses the V0 column
which does not exist. When use.do.call==FALSE, foo2 is called directly.
When use.do.call==TRUE, foo2 is called with the same arguments but using
do.call. Calling foo() takes about 1 second. Calling
foo(use.do.call=TRUE)takes about 20 seconds. Does anybody know what
could explain the difference
in running time? The difference seems to be related to error handling,
since try(foo(use.do.call=TRUE)) takes just 1 second.

We used the latest R version (2.15.0) for the test.

Any insight will be appreciated,

Try traceback(max.lines = 10) after each.

With do.call it gives the error equally soon: there is deparsing to be done in reporting the calls. That is because you passed the object z and not the symbol z: see the help for do.call.

There are better ways to construct calls: including as here not doing so but presumably not in the real problem. See e.g. ?call and ?substitute

I don't really see why you did not post this on R-help: it is entirely about R programming.

Thanks,
Alon


--
Brian D. Ripley,                  rip...@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Reply via email to