我试图弄清楚为什么我保存为.rda的某些数组似乎比相同大小的其他数组占用更多的内存。下面是两个具有相同大小、类型和尺寸的对象x和y。当我保存每一个时,一个是41Mb,另一个是6Mb。有没有人能想到为什么会发生这种情况?
> dim(x)
[1] 71 14 10000
> dim(y)
[1] 71 14 10000
> class(x)
[1] "array"
> class(y)
[1] "array"
> object.size(y)
79520208 bytes
> object.size(x)
79520208 bytes发布于 2011-10-12 11:04:54
如果使用save或saveRDS命令保存,则默认使用压缩。如果你在向量中有不同的内容,它们会有不同的压缩方式...
尝试使用save和compress=FALSE,然后再进行比较...
在下面的示例中,文件大小几乎相差700倍:
set.seed(42)
x <- runif(1e6) # random values should not compress well...
y <- rep(0, 1e6) # zeroes should compress very well...
object.size(x) # 8000040 bytes
object.size(y) # 8000040 bytes
save('x', file='x.rds')
save('y', file='y.rds')
file.info(c('x.rds', 'y.rds'))$size
#[1] 5316773 7838
save('x', file='x.rds', compress=FALSE)
save('y', file='y.rds', compress=FALSE)
file.info(c('x.rds', 'y.rds'))$size
#[1] 8000048 8000048发布于 2011-10-12 09:30:27
它们都可以是字符数组,也可以是列表或数据帧。或者一个可以是字符(一个或两个字节是最小的元素大小,otehr可以是数字(每个元素8个字节),或者较大的字符可以具有较大的字符元素.....或者其他各种可能性。我得到的结果与你的相同:
x <- array(runif( 71* 14 *10000), dim = c(71 , 14, 10000) )
save(x, file="test.rda")
object.size(x)
# 79520208 bytes and the file is over 50 MB
x <- array(sample(letters, 71* 14 *10000, replace=TRUE), dim = c(71 , 14, 10000) )
save(x, file="test2.rda")
object.size(x)
# 79521456 bytes and the file is around 8 MBhttps://stackoverflow.com/questions/7734183
复制相似问题