[R] problem with do.call

ripley@stats.ox.ac.uk ripley at stats.ox.ac.uk
Thu Apr 11 20:32:22 CEST 2002

On Thu, 11 Apr 2002, Thomas Lumley wrote:

> On 11 Apr 2002, Ernesto Jardim wrote:
> > Hi
> >
> > This was not my understanding. I thougth that if you can use functions
> > like apply and similar instead of for loops your code will be faster.
> > Basicly relying on these functions code which is (should be) optimized
> > for speed.
> >
> > If what you're saying is true then using functions like apply is a
> > matter of simplicity and not speeding up the code.
> >
> > Is this correct ?
> >
> Yes. As you can easily verify [and always should verify if you're doing
> optimisation], the apply commands are rarely faster than their for() loop
> equivalents. They can be slower.
> The speed advantage of apply is partly mythical -- there's never been that
> much advantage -- and partly historical, as in some versions of S-PLUS 3.x
> apply was often faster for complicated reasons due to memory management.

I think that is a little pessimistic. It is true for apply() in R, which
just streamlines a for() loop, and also does things you may not want.
However, lapply is an internal function (written by me) because it is
sometimes a lot faster, and in my experiments never slower.

lapply() was a lot faster in S-PLUS 3.4.  It was often slower than for()
in 5.0, hence a lot of consternation.   There *are* a lot of myths about,
but not all in one direction.  As others have said, `S Programming' tries
to give a balanced view across 3 different S implementations, and
profiling can be a great tool in optimizing code (it can be misleading
too, but rarely when it matters).

Summary: lapply is enoouraged.  apply is a matter of style.  Test out
whatever you do to see if it is really worthwhile.

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 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list