[R] Explaining timings (was Computing sums of the columns of an array)

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Aug 5 19:46:54 CEST 2005


On Fri, 5 Aug 2005, Liaw, Andy wrote:

>> 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.

I've explained that to someone else earlier in the week.  You need to get 
R's internal gc limits tuned up to match the task in hand, and on the 
first run it will often gc frequently.  I got 90 GCs the first time, 9 
the second and eventually about 7.

-- 
Brian D. Ripley,                  ripley at 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 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list