附加到 R 中的列表会导致复制吗? [英] Does appending to a list in R result in copying?

查看:22
本文介绍了附加到 R 中的列表会导致复制吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在 R 中创建了一个列表并按如下方式附加到它:

Suppose I have create a list in R and append to it as follows:

x = list(10)
x[[2]] = 20

这等价于

x = list(10)
x = list(10, 20)

?我对 R 如何处理内存中的列表的特定细节没有多少经验,但我有限的理解是它倾向于复制快乐;对我来说最理想的是第一个选项本质上不涉及在内存中创建另一个列表,而只会导致在内存中为附加值留出一个新位置.本质上,如果我有一个很大的列表,我不想让 R 制作另一个副本,如果我只想在其中附加一些内容.

? I'm not so experienced with the particular details of how R handles lists in memory, but my limited understanding is that it tends to be copy-happy; what would be ideal for me would be that the first option doesn't involve essentially creating another list in memory, but just results in setting aside a new place in memory for the appended value. Essentially, if I have a big list, I don't want R to make another copy of it if I just want to append something to it.

如果我想要的行为不是这里给出的,还有其他方法可以获得预期的效果吗?

If the behaviour I want is not what is given here, is there any other way I can get the desired effect?

推荐答案

我相当有信心答案是否定的.我使用以下代码进行仔细检查:

I'm fairly confident the answer is "no". I used the following code to double check:

Rprof(tmp <- tempfile(), memory.profiling = TRUE)

x <- list()
for (i in 1:100) x[[i]] <- runif(10000)

Rprof()
summaryRprof(tmp, memory = "stats")
unlink(tmp)

输出:

# index: runif
#      vsize.small  max.vsize.small      vsize.large  max.vsize.large 
#            76411           381781           424523          1504387 
#            nodes        max.nodes     duplications tot.duplications 
#          2725878         13583136                0                0 
#          samples 
#                5 

相关部分是duplications = 0.

这篇关于附加到 R 中的列表会导致复制吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆