From hadley at rice.edu  Thu Jul 12 19:49:50 2012
From: hadley at rice.edu (Hadley Wickham)
Date: Thu, 12 Jul 2012 10:49:50 -0700
Subject: [Rd] Understanding tracemem
In-Reply-To: <CABdHhvHnS1zxORys7s2v1TB45qUjhkHxHqJ5COvRvGG+enyLkw@mail.gmail.com>
References: <CABdHhvFfhO-FCa94FE7Tm1qrd1fp7y72Zq-rHN8_84rDig__NQ@mail.gmail.com>
	<4FFF02F4.4050400@stats.ox.ac.uk>
	<CABdHhvHDxJWD3OLyYb2HLQPKSbVrA=gg0hk50SMksBVMVgwLdA@mail.gmail.com>
	<4FFF0A87.9030001@stats.ox.ac.uk>
	<CABdHhvHnS1zxORys7s2v1TB45qUjhkHxHqJ5COvRvGG+enyLkw@mail.gmail.com>
Message-ID: <CABdHhvETccpo28Oa1Bab0gpMQA_QJKTfvbe6jiHFh4TDdJY4Zg@mail.gmail.com>

> The list gets copied, but do a and b, or does the new list point to
> the existing locations?  The following test suggests that it's a deep
> copy.
>
> x <- 1:1e7
> z <- list(a = x)
>
> system.time(replicate(100, z$b <- 1L)) / 100
> # ~ 0.05s
> system.time(replicate(100, x[1e6 + 1L] <- 1L)) / 100
> # ~ 0.04s

But that should be

system.time(replicate(100, x[1e7 + 1L] <- 1L)) / 100
# ~0.10s
system.time(replicate(100, z$b <- 1L)) / 100
# ~ 0.04s

which suggests that it's not a deep copy.

But

x <- 1:1e6
z <- list(a = x)
system.time(replicate(100, z$b <- 1L)) / 100
# ~0.005s

which suggests it's not a shallow copy either.


But then neither of those are probably good tests because they modify
in place.  I'll think more.

Hadley

-- 
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/



[Rd] Understanding tracemem
Hadley Wickham