matrixStats icon indicating copy to clipboard operation
matrixStats copied to clipboard

Improve speed of rowwise computations

Open frederikziebell opened this issue 1 year ago • 2 comments

Hi Henrik,

I think this is somewhat related to #200: I noticed that row-wise computations in matrixStats are slower than column-wise one and that this effect worsens with the size of the matrix. I think this could be improved in a similar fashion to the genefilter package, in which the row-wise t-test is about 10% slower than the column-wise for large matrices.

Here's some example code:


library("matrixStats")
library("genefilter")
library("bench")
library("tidyverse")

row_col_ratios <- function(n){
  
  set.seed(1)
  mat <- matrix(rnorm(n*n), ncol=n)
  
  res_means <- bench::mark(
    rowMeans2(mat),
    colMeans2(mat),
    iterations = 10,
    check = F
  )
  res_ttests <- bench::mark(
    rowttests(mat),
    colttests(mat),
    iterations = 10,
    check = F
  )
  
  
  data.frame(
    n,
    row_col_means = as.double(res_means$median[1])/as.double(res_means$median[2]),
    row_col_ttests = as.double(res_ttests$median[1])/as.double(res_ttests$median[2])
  )

}

c(1e2,2e2,5e2,1e3,2e3,5e3,1e4,2e4) %>% 
  map(row_col_ratios) %>% 
  bind_rows() %>% 
  pivot_longer(-n) %>% 
  ggplot(aes(n, value, color = name)) +
    geom_point() +
    geom_path() +
    labs(y = "runtime rowwise / runtime colwise")

image

frederikziebell avatar Sep 02 '23 15:09 frederikziebell