R copies for no apparent reason
Your assumptions are wrong. a full discussion and answer to your question can be found here: http://adv-r.had.co.nz/memory.html This *is* complex and probably off topic for this list. Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
On Thu, Mar 2, 2017 at 7:35 AM, Jiucang Hao <jiucang at goraltrading.com> wrote:
Hi Everyone,
Some R function will make R copy the object AFTER the function call, like
nrow, while some others don't, like sum. For example the following code:
x = as.double(1:1e8)
system.time(x[1] <- 100)
y = sum(x)
system.time(x[1] <- 200) ## Fast (takes 0s), after calling sum
foo = function(x) {
return(sum(x))
}
y = foo(x)
system.time(x[1] <- 300) ## Slow (takes 0.35s), after calling foo
Calling foo is NOT slow, because x isn't copied. However, changing x again
is very slow, as x is copied. My guess is that calling foo will leave a
reference to x, so when changing it after, R makes another copy.
Any one knows why R does this? Even when the function doesn't change x at
all? Thanks.
Regards,
JiuCang Hao
[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.