# Copy. vs. deepcopy vs. .=

**URL:** https://discourse.julialang.org/t/copy-vs-deepcopy-vs/95097
**Category:** General Usage
**Tags:** question, deepcopy
**Created:** [February 23, 2023, 5:40pm UTC](https://discourse.julialang.org/t/copy-vs-deepcopy-vs/95097 "2023-02-23T17:40:21Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![prittjam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/prittjam/32/21267_2.png) [@prittjam](https://discourse.julialang.org/u/prittjam)
#### Post date: [February 23, 2023, 5:40pm UTC](https://discourse.julialang.org/t/copy-vs-deepcopy-vs/95097/1 "2023-02-23T17:40:21Z")

</div>

Despite many threads on this, I’m still confused. I want to avoid allocation but something is not working in my code. My setup is like the one below. Can someone explain the difference in this context,

```julia
 A = (rand(3),rand(3),rand(3))
 B = (rand(3),rand(3),rand(3))

```

option 1 works but I want to avoid it because it allocates:

```julia
B = deepcopy(A)

```

option 2 does not allocate but does not work:

```julia
copy!.(B,A)

```

neither does option 3:

```julia
map((b,a)-> b .= a, B, A)

```

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [February 23, 2023, 5:44pm UTC](https://discourse.julialang.org/t/copy-vs-deepcopy-vs/95097/2 "2023-02-23T17:44:57Z")

</div>

you have a collection of collections, I believe option 3 works:

```julia
julia> @ballocated map((b,a)-> b .= a, B, A) setup = begin A = (rand(3),rand(3),rand(3)); B = (rand(3),rand(3),rand(3))end
0

```

but instead of map, you’re probably looking for `foreach`

```julia
julia> foreach(A,B) do a,b
           b .= a
       end

```

---

<div class="post-metadata">

### Author: ![prittjam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/prittjam/32/21267_2.png) [@prittjam](https://discourse.julialang.org/u/prittjam)
#### Post date: [February 23, 2023, 5:48pm UTC](https://discourse.julialang.org/t/copy-vs-deepcopy-vs/95097/3 "2023-02-23T17:48:59Z")

</div>

option 3 doesn’t, if I make a change to A later, then it also changes B. I don’t know why options 2 or 3 don’t work though. I missing something fundamental. I understand that it is a collection of collections, but I thought the map or broadcast would unwrap the outer collection and let me do an in-place copy on the arrays. It doesn’t seem so, and I am confused.

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [February 23, 2023, 5:50pm UTC](https://discourse.julialang.org/t/copy-vs-deepcopy-vs/95097/4 "2023-02-23T17:50:44Z")

</div>

> [@prittjam](#):
>
> if I make a change to A later, then it also changes B

it does not:

```julia
julia> A = (rand(3),rand(3),rand(3))
([0.44064579753939914, 0.5513894805471519, 0.14531256392609548], [0.7632771815487763, 0.01759682471452173, 0.5437928857351825], [0.4404378190057092, 0.005535005026375717, 0.09501474232898721])

julia> B = (rand(3),rand(3),rand(3));

julia> B[1][1]
0.67551842978281

julia> map((b,a)-> b .= a, B, A);

julia> B[1][1]
0.44064579753939914

julia> A[1][1] = 0.0
0.0

julia> B[1][1]
0.44064579753939914

```

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [February 23, 2023, 6:05pm UTC](https://discourse.julialang.org/t/copy-vs-deepcopy-vs/95097/5 "2023-02-23T18:05:26Z")

</div>

> [@prittjam](#):
>
> `copy!.(B,A)`

I think this does “work”, as you expect:

```julia
julia> A = (zeros(3),zeros(3),zeros(3))
([0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0])

julia> B = (ones(3),ones(3),ones(3))
([1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0])

julia> copy!.(A,B)
([1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0])

julia> A
([1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0])

julia> B
([1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0])

julia> A[1][1] = 5.0
5.0

julia> A
([5.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0])

julia> B
([1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0])

julia> @btime copy!.($A,$B)
  13.516 ns (0 allocations: 0 bytes)
([1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0])

```

---

<div class="post-metadata">

### Author: ![prittjam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/prittjam/32/21267_2.png) [@prittjam](https://discourse.julialang.org/u/prittjam)
#### Post date: [February 23, 2023, 6:11pm UTC](https://discourse.julialang.org/t/copy-vs-deepcopy-vs/95097/6 "2023-02-23T18:11:22Z")

</div>

I believe there is a reassignment in another part of the code that is causing the problem. Thanks.

---

<div class="post-metadata">

### Author: ![prittjam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/prittjam/32/21267_2.png) [@prittjam](https://discourse.julialang.org/u/prittjam)
#### Post date: [February 23, 2023, 9:56pm UTC](https://discourse.julialang.org/t/copy-vs-deepcopy-vs/95097/7 "2023-02-23T21:56:59Z")

</div>

Yes, I’ve confirmed this was the problem. Thanks for the feedback.
