我正在使用包 bigmemory 与 R 中的大型矩阵交互。这适用于大型矩阵,除了 attach.big.matrix()
。函数重新加载用 read.big.matrix()
创建的二进制文件比原来对 read.big.matrix()
的调用慢得多.这是一个例子:
library(bigmemory)
# Create large matrix with 1,000,000 columns
X = matrix(rnorm(1e8), ncol=1000000)
colnames(X) = paste("col", 1:ncol(X))
rownames(X) = paste("row", 1:nrow(X))
# Write to file
write.big.matrix(as.big.matrix(X), "X.txt", row.names=TRUE, col.names=TRUE)
# read into big.matrix and create backing-file for faster loading the second time
A = read.big.matrix("X.txt", header=TRUE, has.row.names=TRUE, type="double", backingfile="X.bin", descriptorfile="X.desc")
# Attach the data based on the backing-file
G = attach.big.matrix("X.desc")
当列数较小(即 1000)时,代码按预期工作并且 attach.big.matrix()
比 read.big.matrix()
快.但是有 1,000,000 列,attach.big.matrix()
慢了 10 倍!
另外,请注意,当没有列名时(即注释掉 colnames(X)
行),此性能问题完全消失,我可以在零时间内附加。这表明瓶颈在解析中 X.desc
应该有更好的方法来 attach.big.matrix()
.
与我的真实数据相比,这个矩阵很小。
或者我可以做些不同的事情吗?
谢谢
系统信息:
Intel Xeon E5-2687W @ 3.10GHz 带 64 Gb RAM
Ubuntu 12.04.2 长期支持版
R 3.0.1
大内存_4.4.3
最佳答案
来自包裹user manual
If x has a huge number of rows and/or columns, then the use of rownames and/or colnames will be extremely memory-intensive and should be avoided.
正如您自己建议的那样,当您避免使用列名时,性能问题就会消失。在我的例子中,我通过将列名保存在一个单独的向量中来解决这个问题,我用它来提取我需要的列的索引。
关于R bigmemory attach.big.matrix 对于非常宽的矩阵来说非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19123685/