# [R] efficiency with "%*%"

Uwe Ligges ligges at statistik.uni-dortmund.de
Thu Jan 26 15:55:55 CET 2006

```Philippe Grosjean wrote:

> Excellent, but...
>
>  > x <- rnorm(1000000)
>  > y <- rnorm(1000000)
>  > system.time(x%*%y)
> [1] 0.03 0.00 0.03   NA   NA
>  > system.time(sum(x*y))
> [1] 0.05 0.00 0.04   NA   NA
>  > system.time(crossprod(x, y))
> [1]  0  0  0 NA NA
>
> So, to paraphrase a well-known contributor on this mailing list:
> "Excellent! So, what did you decided to do during the next 30
> milliseconds you will save by using crossprod() instead of x%*%y?
> (joke)
>
> Best,
>
> Philippe Grosjean
>
> P.S.: Uwe, perhaps you should consider buying a faster computer, isn't
> it? :-()

Well, I use R, you know. It is even fast enough for my 5 year old
laptop. For your super computer, please replace 1e6 by 1e8 in the
example above. ;-)

Uwe

> Uwe Ligges wrote:
>
>> dimitrijoe at ipea.gov.br wrote:
>>
>>
>>> Hi,
>>>
>>> x and y are (numeric) vectors. I wonder if one of the following is more
>>> efficient than the other:
>>>
>>> x%*%y
>>>
>>> or
>>>
>>> sum(x*y)
>>> ?
>>
>>
>>
>> I'd try
>>
>>    x <- rnorm(1000000)
>>    y <- rnorm(1000000)
>>    system.time(x%*%y)
>>    system.time(sum(x*y))
>>
>> and finally (hint, hint!):
>>
>>    system.time(crossprod(x, y))
>>
>> Uwe Ligges
>>
>>
>>
>>> Thanks,
>>> Dimitri Szerman
>>>
>>> ______________________________________________
>>> R-help at stat.math.ethz.ch mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> 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