From bates at stat.wisc.edu  Thu Jan 27 21:33:20 2005
From: bates at stat.wisc.edu (Douglas Bates)
Date: Thu Jan 27 21:33:26 2005
Subject: [Rd] the incredible lightness of crossprod
In-Reply-To: <41F946AD.2020500@bank-banque-canada.ca>
References: <41F907EA.90406@pburns.seanet.com>
	<41F946AD.2020500@bank-banque-canada.ca>
Message-ID: <41F95010.3050006@stat.wisc.edu>

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


[Rd] the incredible lightness of crossprod
Douglas Bates