Re: [Rd] Slow do.call when having an error

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Tue, 29 May 2012 14:15:20 +0100

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,                  ripley_at_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_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Tue 29 May 2012 - 13:18:00 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 29 May 2012 - 22:01:55 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive