[R] multiplying a matrix by a vector

Berend Hasselman bhh at xs4all.nl
Fri Nov 4 19:35:02 CET 2016


> On 4 Nov 2016, at 19:27, Berend Hasselman <bhh at xs4all.nl> wrote:
> 
>> 
>> On 4 Nov 2016, at 16:41, Jeff Newmiller <jdnewmil at dcn.davis.ca.us> wrote:
>> 
>> Sara wins on memory use.
>> 
>> Rui wins on speed.
>> 
>> Bert wins on clarity.
>> 
>> library(microbenchmark)
>> 
>> N <- 1000
>> x <- matrix( runif( N*N ), ncol=N )
>> y <- seq.int( N )
>> 
>> microbenchmark( { t( y * t(x) ) }
>>             , { x %*% diag( y ) }
>>             , { sweep( x, 2, y, `*` ) }
>>             )
>> Unit: milliseconds
>>                       expr       min        lq    median        uq      max neval
>>        {     t(y * t(x)) }  6.659562  7.475414  7.871341  8.182623 47.01105 100
>>      {     x %*% diag(y) }  9.859292 11.014021 11.281334 11.733825 48.79463 100
>> {     sweep(x, 2, y, `*`) } 16.535938 17.682175 18.283572 18.712342 55.47159 100
> 
> 
> I get different results with R3.2.2 on Mac OS X (using reference BLAS).
> 

Correction: I meant R3.3.2

Berend


More information about the R-help mailing list