Last updated: 2017-01-15

Code version: 086c3157516dcce3907fd3fd6857a0aec1f35bfa

Outline

This document simply checks speed of some approaches to vector-matrix multiplication, based on code at [http://stackoverflow.com/questions/18349053/fastest-way-for-multiplying-a-matrix-to-a-vector] and also comparing to method used in current ash code.

Rcpp::sourceCpp('mmult.cpp')
require( microbenchmark )
Loading required package: microbenchmark
n=100000
k = 17
mat = matrix(rnorm(n*k),nrow=n)
v = rnorm(17)
identical(mmult1(v,mat),mmult(mat,v))
[1] TRUE
m <- microbenchmark( mmult1( v , mat ) , mmult(mat,v), sweep(mat, 2, v, FUN = "*") , t( t(mat) * v ) , times = 100L )
  print( m , "relative" , order = "median" , digits = 3 )
Unit: relative
                        expr   min    lq  mean median    uq  max neval
               mmult(mat, v)  1.00  1.00  1.00   1.00  1.00  1.0   100
              mmult1(v, mat)  3.69  3.63  4.14   3.59  3.36 10.0   100
               t(t(mat) * v) 14.74 14.85 16.04  15.51 16.22 16.8   100
 sweep(mat, 2, v, FUN = "*") 20.90 20.96 21.70  21.15 20.54 67.8   100

Session information

R version 3.3.1 (2016-06-21)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.11.5 (El Capitan)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] microbenchmark_1.4-2.1 ashr_2.1               workflowr_0.2.0       
[4] rmarkdown_1.3         

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.8       rstudioapi_0.6    knitr_1.15.1     
 [4] magrittr_1.5      REBayes_0.73      MASS_7.3-45      
 [7] munsell_0.4.3     doParallel_1.0.10 pscl_1.4.9       
[10] colorspace_1.2-7  SQUAREM_2016.8-2  lattice_0.20-34  
[13] foreach_1.4.3     plyr_1.8.4        stringr_1.1.0    
[16] tools_3.3.1       parallel_3.3.1    grid_3.3.1       
[19] gtable_0.2.0      git2r_0.18.0      htmltools_0.3.5  
[22] iterators_1.0.8   assertthat_0.1    yaml_2.1.14      
[25] rprojroot_1.1     digest_0.6.10     Matrix_1.2-7.1   
[28] ggplot2_2.1.0     codetools_0.2-15  evaluate_0.10    
[31] stringi_1.1.2     scales_0.4.0      Rmosek_7.1.2     
[34] backports_1.0.4   truncnorm_1.0-7  

This site was created with R Markdown