s-news
[Top] [All Lists]

RE: [S] Yet Another Vectorization Question Answered!

To: "CHASALOW, SCOTT [AG/2165]" <SCOTT.CHASALOW@cereon.com>
Subject: RE: [S] Yet Another Vectorization Question Answered!
From: Prof Brian D Ripley <ripley@stats.ox.ac.uk>
Date: Fri, 24 Sep 1999 07:28:46 +0100 (BST)
Cc: "'Kim Elmore'" <elmore@nssl.noaa.gov>, "'S-NEWS'" <s-news@wubios.wustl.edu>
In-reply-to: <8D7A3D2453C7D2119CD800A0C9EAF097751078@ems2165-01.monsanto.com>
Sender: owner-s-news@wubios.wustl.edu
On Thu, 23 Sep 1999, CHASALOW, SCOTT [AG/2165] wrote:

> 
> Kim,
> 
> In the old days, before colSums() existed, and when lapply()
> was substantially slower than it is now, I would use this:
> 
> rep(1, nrow(dfr)) %*% (dfr < 0)
> 
> which gives the number of negative values in each column.
> 
> Matrix multiplication can be a great tool in vectorizing 
> things.  I *thought* this still would be the most efficient
> of all the options (and was therefore a little surprised
> by Bill Dunlap's response).  I was wrong.  Here's a wee test in

[...]

I did send a reply to Kim Elmore yesterday, with comparative timings. If
you start with a matrix, I found this fastest on 3.4 and 5.1. If you start
with a data frame, unlist(lapply was faster. On the example I tested, the
matrix/data frame coercions were a signifcant part of the time.  On your
(smaller) example colSums is fastest on 5.1 (but almost the same as the
matrix multiplication), starting from a data frame.

What is efficient often depends critically on the engine in use (down to
the release number for 5.0).  Bill Venables and I have a set of worked
examples in our (almost finished) second book, and the relative
efficiencies surprised us, not least because they were quite different from
those on the (earlier versions of these examples) done for our Washington
workshop. It is not just S3 vs S4: S-PLUS 2000 and 3.4 differ markedly
too (and not always in favour of 2000).

If efficiency really matters, we know of no way to find out what is best
but to try all the ideas (although asking Bill Dunlap always springs to
mind).

-- 
Brian D. Ripley,                  ripley@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

-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu.  To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message:  unsubscribe s-news

<Prev in Thread] Current Thread [Next in Thread>