Ric*_*rta 12 benchmarking r matrix min
从矩阵中的每列中提取min的最快方法是什么?
将所有基准移至下面的答案.
  ##  TEST DATA
  set.seed(1)
  matrix.inputs <- list(
        "Square Matrix"     = matrix(sample(seq(1e6), 4^2*1e4, T), ncol=400),   #  400 x  400
        "Tall Matrix"       = matrix(sample(seq(1e6), 4^2*1e4, T), nrow=4000),  # 4000 x   40
        "Wide-short Matrix" = matrix(sample(seq(1e6), 4^2*1e4, T), ncol=4000),  #   40 x 4000
        "Wide-tall Matrix"  = matrix(sample(seq(1e6), 4^2*1e5, T), ncol=4000),   #  400 x 4000
        "Tiny Sq Matrix"    = matrix(sample(seq(1e6), 4^2*1e2, T), ncol=40)     #   40 x   40
  )
Ben*_*ker 10
该sos套餐非常适合回答这些问题.
library("sos")
findFn("colMins")
library("matrixStats")
?colMins
http://finzi.psych.upenn.edu/R/library/matrixStats/html/rowRanges.html
奇怪的是,我试过的一个例子colMins比较慢.也许有人可以指出我的例子有什么好笑的?
set.seed(101); z <- matrix(runif(1e6),nrow=1000)
library(rbenchmark)
benchmark(colMins(z),apply(z,2,min))
##               test replications elapsed relative user.self sys.self
## 2 apply(z, 2, min)          100  14.290     1.00     7.216    7.057
## 1       colMins(z)          100  25.585     1.79    15.509    9.852
这是在正方形和宽矩阵上更快的一个.它用于pmin矩阵的行.(如果您知道将矩阵拆分为行的更快方法,请随时编辑)
do.call(pmin, lapply(1:nrow(mat), function(i)mat[i,]))
使用与@RicardoSaporta相同的基准:
$`Square Matrix`
          test elapsed relative
3 pmin.on.rows   1.370    1.000
1          apl   1.455    1.062
2         cmin   2.075    1.515
$`Wide Matrix`
      test elapsed relative
3 pmin.on.rows   0.926    1.000
2         cmin   2.302    2.486
1          apl   5.058    5.462
$`Tall Matrix`
          test elapsed relative
1          apl   1.175    1.000
2         cmin   2.126    1.809
3 pmin.on.rows   5.813    4.947
更新2014-12-17:
colMins()等.在最新版本的matrixStats中显着提高了速度.这是使用matrixStats 0.12.2的更新基准摘要,显示它("cmin")比第二快速方法快〜5-20倍:
$`Square Matrix`
     test elapsed relative
2    cmin   0.216    1.000
1     apl   4.200   19.444
5 pmn.int   4.604   21.315
4     pmn   5.136   23.778
3    lapl  12.546   58.083
$`Tall Matrix`
     test elapsed relative
2    cmin   0.262    1.000
1     apl   3.006   11.473
5 pmn.int  18.605   71.011
3    lapl  22.798   87.015
4     pmn  27.583  105.279
$`Wide-short Matrix`
     test elapsed relative
2    cmin   0.346    1.000
5 pmn.int   3.766   10.884
4     pmn   3.955   11.431
3    lapl  13.393   38.708
1     apl  19.187   55.454
$`Wide-tall Matrix`
     test elapsed relative
2    cmin   5.591    1.000
5 pmn.int  39.466    7.059
4     pmn  40.265    7.202
1     apl  67.151   12.011
3    lapl 158.035   28.266
$`Tiny Sq Matrix`
     test elapsed relative
2    cmin   0.011    1.000
5 pmn.int   0.135   12.273
4     pmn   0.178   16.182
1     apl   0.202   18.364
3    lapl   0.269   24.455
上
 
一条评论2013-10-09:由于matrixStats v0.8.7(2013-07-28),colMins()其速度大约是之前的两倍.原因在于之前使用的功能colRanges(),这解释了此处报告的"令人惊讶的缓慢"结果.可以看到相同的速度colMaxs(),rowMins()和rowMaxs().
| 归档时间: | 
 | 
| 查看次数: | 7311 次 | 
| 最近记录: |