Duncan Murdoch wrote: > On 8/5/2005 12:16 PM, Martin C. Martin wrote: > >>Hi, >> >>I have a 5x731 array A, and I want to compute the sums of the columns. >>Currently I do: >> >>apply(A, 2, sum) >> >>But it turns out, this is slow: 70% of my CPU time is spent here, even >>though there are many complicated steps in my computation. >> >>Is there a faster way? > > > You'd probably do better with matrix multiplication: > > rep(1, nrow(A)) %*% A
No, better use colSums(), which has been optimized for this purpose: A <- matrix(seq(1, 10000000), ncol=10000) system.time(colSums(A)) # ~ 0.1 sec. system.time(rep(1, nrow(A)) %*% A) # ~ 0.5 sec. Uwe Ligges > Duncan Murdoch > > ______________________________________________ > [email protected] mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html ______________________________________________ [email protected] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
