Crossprod比%*%慢,为什么?

时间:2019-11-06 00:19:09

标签: r rstudio lapack blas cross-product

在减少过去几天一直在编码的算法的计算时间的各种尝试中,我想测试crossprod%*%的有效改进。我惊讶地注意到,使用%*%,我的算法将运行得更快。

因此,我决定在普通矩阵上使用microbenchmark()(以及system.time())比较这两个例程,并得到以下结果:

M <- 1000
K <- 100
A <- matrix(rnorm(M*K, 10, 1), ncol = M)
b <- rnorm(K, 10, 1)
B <- matrix(rnorm(M*K, 10, 1), ncol = M)

microbenchmark(crossprod(A, A), t(A)%*%A, times = 1000, unit="ms")
Unit: milliseconds
            expr       min        lq     mean    median       uq      max neval cld
 crossprod(A, A) 112.58885 121.05406 149.8290 129.31873 147.6489 358.3164  1000   b
      t(A) %*% A  76.77698  81.68934 108.0526  89.50015 105.9617 304.7395  1000  a 

microbenchmark(crossprod(A), t(A)%*%A, times = 1000, unit="ms")
Unit: milliseconds
         expr      min       lq     mean   median       uq      max neval cld
 crossprod(A) 58.26374 61.56330 69.35781 64.65561 71.42403 314.9268  1000  a 
   t(A) %*% A 76.97771 81.80069 92.21863 85.75894 93.50332 273.5133  1000   b

microbenchmark(crossprod(A, B), t(A)%*%B, times = 1000, unit="ms")
Unit: milliseconds
            expr       min       lq      mean    median        uq      max neval cld
 crossprod(A, B) 109.27471 111.6751 118.00118 112.97533 117.55815 284.6910  1000   b
      t(A) %*% B  74.36276  77.0441  83.33582  77.89172  82.58609 258.3154  1000  a

microbenchmark(crossprod(A, b), t(A)%*%b, times = 1000, unit="ms")
Unit: milliseconds
            expr      min        lq      mean    median       uq       max neval cld
 crossprod(A, b) 0.149644 0.1553795 0.1884534 0.1577500 0.167333  6.737466  1000  a 
      t(A) %*% b 0.338180 0.6239705 0.8052485 0.6423505 0.678017 13.011479  1000   b

microbenchmark(crossprod(b, d), t(b)%*%d, times = 1000, unit="ms")
Unit: milliseconds
            expr      min        lq        mean   median        uq      max neval cld
 crossprod(b, d) 0.000814 0.0009130 0.001153643 0.000973 0.0010740 0.018912  1000  a 
      t(b) %*% d 0.002547 0.0029275 0.003554290 0.003087 0.0033005 0.057184  1000   b

microbenchmark(crossprod(b), t(b)%*%b, times = 1000, unit="ms")
Unit: milliseconds
         expr      min       lq        mean   median       uq      max neval cld
 crossprod(b) 0.000758 0.000801 0.000866091 0.000848 0.000883 0.004277  1000  a 
   t(b) %*% b 0.002546 0.002686 0.002872259 0.002785 0.002898 0.033779  1000   b

显然,至少在我的机器上,crossprod仅在处理向量或采用矩阵平方而不指定y参数时才更快。

我知道从理论上讲,crossprod通常应该更快,所以这怎么可能?

sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Manjaro Linux

Matrix products: default
BLAS:   /usr/lib/libblas.so.3.8.0
LAPACK: /usr/lib/liblapack.so.3.8.0

0 个答案:

没有答案