From ripley at stats.ox.ac.uk  Tue Mar 11 14:41:05 2008
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Tue, 11 Mar 2008 13:41:05 +0000 (GMT)
Subject: [Rd] crossprod is slower than t(AA)%*BB
In-Reply-To: <EA09C4B2B0F16E44B8F3311629493C0D03B26E69@DJFPOST01.djf.agrsci.dk>
References: <EA09C4B2B0F16E44B8F3311629493C0D03B26E69@DJFPOST01.djf.agrsci.dk>
Message-ID: <alpine.LFD.1.00.0803111308020.14388@gannet.stats.ox.ac.uk>

Please, you can't compare calls starting with different memory settings
(and the garbage collector does adjust to recent usage).  Try 
them in the other order or in parallel sessions.

>From the times you quote it looks to me as if you would benefit from using 
an optimized BLAS.  Here are some results on a dual core 64-bit Linux 
system with the Goto BLAS and one thread, starting from the same point (a 
fresh session loading a saved workspace containing AA and BB)

> system.time(t(AA)%*%BB,gcFirst=TRUE)
    user  system elapsed
   4.304   0.046   4.351
> system.time(t(AA)%*%BB,gcFirst=TRUE)
    user  system elapsed
   5.026   0.053   5.082
> system.time(t(AA)%*%BB,gcFirst=TRUE)
    user  system elapsed
   4.992   0.033   5.028
> system.time(t(AA)%*%BB,gcFirst=TRUE)
    user  system elapsed
   4.979   0.043   5.026
> system.time(t(AA)%*%BB,gcFirst=TRUE)
    user  system elapsed
   5.012   0.030   5.046

and in another session

> system.time(crossprod(AA,BB),gcFirst=TRUE)
    user  system elapsed
   4.616   0.026   4.644
> system.time(crossprod(AA,BB),gcFirst=TRUE)
    user  system elapsed
   4.791   0.000   4.817
> system.time(crossprod(AA,BB),gcFirst=TRUE)
    user  system elapsed
   4.764   0.000   4.779
> system.time(crossprod(AA,BB),gcFirst=TRUE)
    user  system elapsed
   4.092   0.000   4.095
> system.time(crossprod(AA,BB),gcFirst=TRUE)
    user  system elapsed
   4.824   0.000   4.834

Note how there is a lot of variability depending on the initial memory 
state.  I would be surprised if the differences you are seeing exceed the 
reproducibilty spreads.

BTW: gcFirst=TRUE is the default, and I get a considerable speed up (but 
even more variability) by allowing multiple threads.


On Mon, 10 Mar 2008, Ole Fredslund Christensen wrote:

> Dear Rdevelopers
>
> The background for this email is that I was helping a PhD student to
> improve the speed of her R code. I suggested to replace calls like
> t(AA)%*% BB by crossprod(AA,BB) since I expected this to be faster. The
> surprising result to me was that this change actually made her code
> slower.
>
>
>> ## Examples :
>>
>> AA <- matrix(rnorm(3000*1000),3000,1000)
>> BB <- matrix(rnorm(3000^2),3000,3000)
>> system.time(crossprod(AA,BB),gcFirst=TRUE)
>   user  system elapsed
>  24.58    0.06   24.69
>> system.time(t(AA)%*%BB,gcFirst=TRUE)
>   user  system elapsed
>  23.25    0.04   23.32
>>
>>
>> AA <- matrix(rnorm(2000^2),2000,2000)
>> BB <- matrix(rnorm(2000^2),2000,2000)
>> system.time(crossprod(AA,BB),gcFirst=TRUE)
>   user  system elapsed
>  21.94    0.03   21.98
>> system.time(t(AA)%*%BB,gcFirst=TRUE)
>   user  system elapsed
>  21.16    0.02   21.19
>>
>>
>> version
>               _
> platform       i386-pc-mingw32
> arch           i386
> os             mingw32
> system         i386, mingw32
> status
> major          2
> minor          6.2
> year           2008
> month          02
> day            08
> svn rev        44383
> language       R
> version.string R version 2.6.2 (2008-02-08)
>
>
> Clearly there are many examples where crossprod is indeed faster than
> t(x)%*%y,
> but I suggest to change the wording in the help file for crossprod such
> that it says
> ".... formally equivalent (but often faster than) the call t(x)%*%y ...
> ".
>
>
> Yours
>
> Ole Christensen
>
>
>
>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

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



[Rd] crossprod is slower than t(AA)%*BB
Prof Brian Ripley