I create two matrices A
and B
of the same dimension. A
contains larger values than B
. The matrix multiplication A %*% A
is about 10 times faster than B %*% B
.
Why is this?
## disable openMP
library(RhpcBLASctl); blas_set_num_threads(1); omp_set_num_threads(1)
A <- exp(-as.matrix(dist(expand.grid(1:60, 1:60))))
summary(c(A))
# Min. 1st Qu. Median Mean 3rd Qu. Max.
# 0.000000 0.000000 0.000000 0.001738 0.000000 1.000000
B <- exp(-as.matrix(dist(expand.grid(1:60, 1:60)))*10)
summary(c(B))
# Min. 1st Qu. Median Mean 3rd Qu. Max.
# 0.0000000 0.0000000 0.0000000 0.0002778 0.0000000 1.0000000
identical(dim(A), dim(B))
## [1] TRUE
system.time(A %*% A)
# user system elapsed
# 2.387 0.001 2.389
system.time(B %*% B)
# user system elapsed
# 21.285 0.020 21.310
sessionInfo()
# R version 3.6.1 (2019-07-05)
# Platform: x86_64-pc-linux-gnu (64-bit)
# Running under: Linux Mint 19.2
# Matrix products: default
# BLAS: /usr/lib/x86_64-linux-gnu/openblas/libblas.so.3
# LAPACK: /usr/lib/x86_64-linux-gnu/libopenblasp-r0.2.20.so
The question could be related to R: base::chol() slows down when matrix contains many small entries.