Re: [Rd] Slow when having an error

From: Prof Brian Ripley <>
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 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, = 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 (
>, list(z))
> else
> foo2(z)
> }
> foo2, when called, generates an error because it accesses the V0 column
> which does not exist. When, foo2 is called directly.
> When, foo2 is called with the same arguments but using
> Calling foo() takes about 1 second. Calling
> foo( 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( 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 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

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,        
Professor of Applied Statistics,
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________ mailing list
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 Please read the posting guide before posting to the list.

list of date sections of archive