为什么在大型稀疏矩阵上的R行提取比将其分成较小的部分然后提取时要慢?
我正在使用“dgCMatrix”类的 19089 x 9432 稀疏矩阵(我们称之为M),我必须提取每一行以对其执行一些计算。我用一个循环来做这件事,所以在每次迭代时,我必须做一些事情,比如currentrow <- M[i,]在它上面应用循环的主体。该计算是非常耗时的,所以我想它优化尽可能多地,我意识到,如果我第一分我的矩阵小块(M[1:100,],M[101:200,],等...),而我做一个循环在每个那些规模较小矩阵(因此currentrow <- current_smallM[i,]在每次迭代时调用),循环要快得多。
这是我运行以重现此代码示例:
library(Matrix)
N = 10000
M = 5000
# Creation of the large matrix (of class dgCMatrix)
largeMatrix <- Matrix(rnorm(N*M,mean=0,sd=1), byrow = TRUE, nrow = N, sparse = TRUE)
# We take into account the time for the creation of the smaller matrix, and then calculate the time to allocate the 200 rows to a variable
start.time = Sys.time()
smallMatrix = largeMatrix[100:200,]
for (i in 1:100){
test <- smallMatrix[i,]
}
end.time = Sys.time()
print(end.time - start.time) # 0.47 secs
# Same allocations but working on the large matrix
start.time = Sys.time()
for (i in 100:200){
test <- largeMatrix[i,]
}
end.time = Sys.time()
print(end.time - start.time) # 18.44 secs
你可以看到时差真的很大......所以我真的很想知道:
- 为什么会这样?
- 有没有比将我的矩阵分成更小的部分更有效的方法来存储我的数据?
有趣的是,我用一个matrix对象(使用largeMatrix <- matrix( rnorm(N*M,mean=0,sd=1), N, M))测试了相同的代码,结果完全不同:分割矩阵为 0.06 秒,大矩阵为 0.04 秒,所以我真的想知道稀疏表示有什么不同。
注意:我在这里发现了一个非常相似的问题,但它使用的是不同的语言,并且(我认为)解决方案在这里不适用,因为它是由于隐式类型转换,而在这里我只是提取一行。
感谢您的帮助!