On Fri, 2005-08-05 at 12:16 -0400, 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?
Yes, colSums() e.g.: > set.seed(1234) > dat <- matrix(runif(5*731), ncol = 731) > system.time(for(i in 1:1000) apply(dat, 2, sum), gcFirst = TRUE) [1] 8.05 0.00 9.89 0.00 0.00 > system.time(for(i in 1:1000) colSums(dat), gcFirst = TRUE) [1] 0.09 0.01 0.09 0.00 0.00 But neither is that slow on my system. What is A? HTH Gav -- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Gavin Simpson [T] +44 (0)20 7679 5522 ENSIS Research Fellow [F] +44 (0)20 7679 7565 ENSIS Ltd. & ECRC [E] gavin.simpsonATNOSPAMucl.ac.uk UCL Department of Geography [W] http://www.ucl.ac.uk/~ucfagls/cv/ 26 Bedford Way [W] http://www.ucl.ac.uk/~ucfagls/ London. WC1H 0AP. %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% ______________________________________________ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html