[Rd] the incredible lightness of crossprod

Douglas Bates bates at stat.wisc.edu
Thu Jan 27 21:33:20 CET 2005


Paul Gilbert wrote:
> A few weeks ago I noticed
> 
>  >  z <- matrix(rnorm(20000),10000,2)
> 
>  > system.time(for (i in 1:1000) apply(z,2,sum))
> [1] 13.44  0.48 14.08  0.00  0.00
> 
>  > system.time(for (i in 1:1000) rep(1,10000) %*% z)
> [1] 6.46 0.11 6.84 0.00 0.00
> 
> which seemed  completely contrary to all my childhood teachings. Now

Must have had an interesting childhood if you spent it learning about 
the speeds of various matrix multiplication techniques.
	
>  > system.time(for (i in 1:1000) crossprod(rep(1,10000), z))
> [1] 1.90 0.12 2.24 0.00 0.00

and there is a good chance that a significant portion of that time is 
taken up by repeating the rep(1, 10000) function call 1000 times.

> makes sense because it is suppose to be faster than %*% , but why is 
> apply so slow?

I believe that this is Bert Gunter's cue to comment on the internal (or 
infernal) nature of the apply functions.

> (And should I go back and change apply in my code everywhere or is this 
> likely to reverse again?)
> 
> Paul Gilbert



More information about the R-devel mailing list