在编程或数学上有任何建议,以加快R?我包括了一些生成的数据,这些数据与我正在使用的真实数据方案非常匹配。我还试图使用应用和拍摄,并试图将其变成一个稀疏的矩阵,因为它具有很多0,但到目前为止,这是我提出的最快的方法。有什么建议使其更快吗?我需要进行10,000次计算。
数据与我的方案紧密匹配:
set.seed(7)
# same size matrix as my real data data puzzle
A <- matrix(rbeta((13163*13163),1,1), ncol = 13163, nrow = 13163)
# turn a bunch to 0 to more closely match that I have a lot of 0's in real data
A[A < 0.5] <- 0
# create binary matrix
z <- matrix(rbinom((13163*13163), 1, 0.25), ncol = 13163, nrow = 13163)
我发现rfast :: rowsums为我提供了最快的结果。
start1 <- Sys.time()
testA <- 1 - exp(Rfast::rowsums(log(1-A*z)))
stop1 <- Sys.time()
stop1 - start1
赦免我笨拙的基准测试方法...
您可以摆脱exp()
和log()
:
testB <- 1 - Rfast::rowprods(1-A*z)
这快8倍。
然而,当您将许多数字乘以0到1之间,最终到处都有0,因此输出向量为1s。