[R] colSums in C

Thomas Lumley tlumley at u.washington.edu
Tue Dec 18 01:32:34 CET 2001

On Mon, 17 Dec 2001, David Brahm wrote:

> I asked how to write speedy C code to implement colSums().  My original version
> on a 400x40000 matrix took 5.72s.
> Peter Dalgaard <p.dalgaard at biostat.ku.dk> suggested some more efficient coding,
> which sped my example up to 3.90s.  Douglas Bates <bates at stat.wisc.edu>
> suggested using .Call() instead of .C, and I was amazed to see the time went
> down to 0.69s!  Doug had actually posted his code (a package called "MatUtils")
> to R-help on July 19, 2001.
> I've taken Doug's code, added names to the result, and included an na.rm flag.
> Unfortunately, my na.rm option makes it really slow again! (12.15s).  That's no
> faster than pre-processing the matrix with "m[is.na(m)] <- 0".  Can anyone help
> me understand why the ISNA conditional is taking so much time?  The C code is
> below.  Thanks!

I don't find as much slow-down as you do.  I also get about 0.6s with
na.rm=FALSE but only about 2.3s with na.rm=TRUE, which is more reasonable
(this is Linux, Pentium III).

The ISNA() macro does expand to two function calls, which you would expect
to be quite a bit slower than a single addition.


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