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? Thanks, Martin
See ?colSums. Andy> From: Martin C. Martin > > 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? > > Thanks, > Martin > > ______________________________________________ > R-help at 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 > > >
colSums() is a lot faster. Reid Huntsinger -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Martin C. Martin Sent: Friday, August 05, 2005 12:17 PM To: r-help at stat.math.ethz.ch Subject: [R] Computing sums of the columns of an array 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? Thanks, Martin ______________________________________________ R-help at 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
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 Duncan Murdoch
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)colSums(A) Uwe Ligges> 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? > > Thanks, > Martin > > ______________________________________________ > R-help at 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
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. %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Uwe Ligges > Sent: Friday, August 05, 2005 12:44 PM > To: Duncan Murdoch > Cc: r-help at stat.math.ethz.ch > Subject: Re: [R] Computing sums of the columns of an array > > > 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.With the dimension that Martin stated, I don't see much difference:> A <- matrix(runif(5 * 731), 5) > system.time(replicate(1e4, rep(1, nrow(A)) %*% A), gcFirst=TRUE)[1] 5.28 0.13 5.46 NA NA> system.time(replicate(1e4, rep(1, nrow(A)) %*% A), gcFirst=TRUE)[1] 1.99 0.20 2.22 NA NA> system.time(replicate(1e4, rep(1, nrow(A)) %*% A), gcFirst=TRUE)[1] 1.97 0.25 2.28 NA NA> system.time(replicate(1e4, rep(1, nrow(A)) %*% A), gcFirst=TRUE)[1] 1.90 0.20 2.16 NA NA> system.time(replicate(1e4, colSums(A)), gcFirst=TRUE)[1] 1.53 0.22 1.75 NA NA> system.time(replicate(1e4, colSums(A)), gcFirst=TRUE)[1] 1.53 0.19 1.72 NA NA> system.time(replicate(1e4, colSums(A)), gcFirst=TRUE)[1] 1.51 0.19 1.70 NA NA> system.time(replicate(1e4, colSums(A)), gcFirst=TRUE)[1] 1.49 0.25 1.79 NA NA However, I don't understand why the first try took so much longer. Andy> Uwe Ligges > > > > > Duncan Murdoch > > > > ______________________________________________ > > R-help at 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 > > > ______________________________________________ > R-help at 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 > > >