[R] distance between two matrices

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Jan 28 18:50:13 CET 2004

On Wed, 28 Jan 2004, "Hüsing, Johannes" wrote:

> > From: Prof Brian Ripley [mailto:ripley at stats.ox.ac.uk]
> > On Wed, 28 Jan 2004, "Hüsing, Johannes" wrote:
> > > ?apply for more info. You'll want to know about apply if
> > > you want to avoid loops (which is a good approach).
> > 
> > Unfortunately apply() is a wrapper for a for() loop, so will 
> > not help much 
> > (if at all).
> whoops; so is the choice between both a matter of style? Or is
> it implementation-specific for R, and not generally true for S?

It is not generally true for S.  The case studies in chapter 7 of `S 
Programming' show that efficient ways are implementation-dependent.

In some versions of S-PLUS (e.g. 3.4) apply was much faster and in some
(5.0) it was much slower.  Using lapply() (which has a C-level loop) is
sometimes rather faster in R.

In this particular example garbage collection is taking about 50% of time
of the pure R solution, so exactly what is done in what order can matter.  
knn1 is 20-30x faster since it works in place with space allocated just

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

More information about the R-help mailing list