# Assignments, Copies and Deepcop

**URL:** https://discourse.julialang.org/t/assignments-copies-and-deepcop/30723
**Category:** Performance
**Created:** [November 5, 2019, 2:36am UTC](https://discourse.julialang.org/t/assignments-copies-and-deepcop/30723 "2019-11-05T02:36:31Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![natgeo-wong](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/natgeo-wong/32/10965_2.png) [@natgeo-wong](https://discourse.julialang.org/u/natgeo-wong)
#### Post date: [November 5, 2019, 2:36am UTC](https://discourse.julialang.org/t/assignments-copies-and-deepcop/30723/1 "2019-11-05T02:36:31Z")

</div>

This is a question regarding the use of “=” signs in Julia.

I can do the following for-loop in MATLAB

```julia
for ii = 1 : dt
    ηp1 = shallowwave1D(ηn,ηm1,r);
    ηm1 = ηn; ηn = ηp1; 
end

```

Can the same be done in Julia? Or will there be some issues with copies where whatever I do to ηn changes ηm1 (see the second statement in the second line of the for-loop)?

Just asking, because this has tripped me up multiple times in Python where I needed to do copying to resolve the issue.

Do I need to do

```julia
for ii = 1 : dt
    ηp1 = shallowwave1D(ηn,ηm1,r);
    ηm1 = deepcopy(ηn); ηn = deepcopy(ηp1); 
end

```

such that they are all independent of each other?

---

<div class="post-metadata">

### Author: ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)
#### Post date: [November 5, 2019, 2:43am UTC](https://discourse.julialang.org/t/assignments-copies-and-deepcop/30723/2 "2019-11-05T02:43:02Z")

</div>

```julia
for ii = 1 : dt
    ηp1 = shallowwave1D(ηn,ηm1,r);
    ηm1 = ηn; ηn = ηp1; 
end

```

I am really confused about this loop. Why is the looping variable ii not used inside the loop itself?

---

<div class="post-metadata">

### Author: ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)
#### Post date: [November 5, 2019, 2:46am UTC](https://discourse.julialang.org/t/assignments-copies-and-deepcop/30723/3 "2019-11-05T02:46:46Z")

</div>

```julia
for ii = 1 : dt
    ηp1 = shallowwave1D(ηn,ηm1,r);
    ηm1 = deepcopy(ηn); ηn = deepcopy(ηp1); 
end

```

It would really help if you can tell us what is

1. ηp1
2. ηn

Is it a scalar (ie Float64), a struct , an array , a Dict or a set?

---

<div class="post-metadata">

### Author: ![natgeo-wong](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/natgeo-wong/32/10965_2.png) [@natgeo-wong](https://discourse.julialang.org/u/natgeo-wong)
#### Post date: [November 5, 2019, 2:47am UTC](https://discourse.julialang.org/t/assignments-copies-and-deepcop/30723/4 "2019-11-05T02:47:57Z")

</div>

They are all supposed to be numeric arrays (i.e. something like [1,2,3,4]).

---

<div class="post-metadata">

### Author: ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)
#### Post date: [November 5, 2019, 2:50am UTC](https://discourse.julialang.org/t/assignments-copies-and-deepcop/30723/5 "2019-11-05T02:50:02Z")

</div>

If they are arrays of arrays then use deepcopy to be really safe

if they are just arrays of scalars (ie array of Float64), you can use copy

---

<div class="post-metadata">

### Author: ![natgeo-wong](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/natgeo-wong/32/10965_2.png) [@natgeo-wong](https://discourse.julialang.org/u/natgeo-wong)
#### Post date: [November 5, 2019, 2:51am UTC](https://discourse.julialang.org/t/assignments-copies-and-deepcop/30723/6 "2019-11-05T02:51:19Z")

</div>

Noted! Thanks!  
If I didn’t use copy or deepcopy, what would happen?

---

<div class="post-metadata">

### Author: ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)
#### Post date: [November 5, 2019, 2:52am UTC](https://discourse.julialang.org/t/assignments-copies-and-deepcop/30723/7 "2019-11-05T02:52:25Z")

</div>

> Noted! Thanks!  
> If I didn’t use copy or deepcopy, what would happen?

Then you get aliasing

julia\> a = [1 2 3]  
1×3 Array{Int64,2}:  
1 2 3

julia\> b = a  
1×3 Array{Int64,2}:  
1 2 3

julia\> b[2]=666  
666

julia\> b  
1×3 Array{Int64,2}:  
1 666 3

julia\> a  
1×3 Array{Int64,2}:  
1 666 3

```julia

```

---

<div class="post-metadata">

### Author: ![bashonubuntu](https://avatars.discourse-cdn.com/v4/letter/b/f19dbf/32.png) [@bashonubuntu](https://discourse.julialang.org/u/bashonubuntu)
#### Post date: [November 5, 2019, 3:06am UTC](https://discourse.julialang.org/t/assignments-copies-and-deepcop/30723/8 "2019-11-05T03:06:46Z")

</div>

May also be helpful to see this behavior

```julia
julia> a = [1, 2, 3]
3-element Array{Int64,1}:
 1
 2
 3

julia> b = a[:]
3-element Array{Int64,1}:
 1
 2
 3

julia> b[1]
1

julia> b[1] = 4
4

julia> b
3-element Array{Int64,1}:
 4
 2
 3

julia> a
3-element Array{Int64,1}:
 1
 2
 3

```

In the above example, a does not change when we change b. You could also check out point #5 in

> **[7 Julia Gotchas and How to Handle Them - Stochastic Lifestyle](http://www.stochasticlifestyle.com/7-julia-gotchas-handle/)**
>
> Let me start by saying Julia is a great language. I love the language, it is what I find to be the most powerful and intuitive language that I have ever used. It’s undoubtedly my favorite language. That said, there are some “gotchas”, tricky little...

---

<div class="post-metadata">

### Author: ![natgeo-wong](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/natgeo-wong/32/10965_2.png) [@natgeo-wong](https://discourse.julialang.org/u/natgeo-wong)
#### Post date: [November 5, 2019, 4:07am UTC](https://discourse.julialang.org/t/assignments-copies-and-deepcop/30723/9 "2019-11-05T04:07:53Z")

</div>

Thank you so much for the help!

---

<div class="post-metadata">

### Author: ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)
#### Post date: [November 5, 2019, 5:21am UTC](https://discourse.julialang.org/t/assignments-copies-and-deepcop/30723/10 "2019-11-05T05:21:35Z")

</div>

In this specific case, you don’t need to copy.

`ηn = ηp1` does not _mutate_ `ηn`, but _re-binds_ it to another array. So, `ηm1` is not affected.  
It would only be affected if you modified `ηn` in-place, e.g. with `copyto!(ηn, ηp1)`.

---

<div class="post-metadata">

### Author: ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)
#### Post date: [November 5, 2019, 2:09pm UTC](https://discourse.julialang.org/t/assignments-copies-and-deepcop/30723/11 "2019-11-05T14:09:19Z")

</div>

In addition to @Vasily_Pisarev’s answer (with which I fully agree), I would like to add that I think a common idiom to perform what (I think) you want to do would be something like:

```julia
# Initialize input arrays
ηm1 = something
ηn = something

# Preallocate an array for the result
ηp1 = similar(ηn)

# No need to have a named iteration variable if you don't use it:
# naming the variable "_" makes it clear that it is not used
for _ = 1 : dt
  # shallowwave1D is rewritten in such a way that it mutates ηp1
  # instead of creating a new array (hence the "!" suffix)
  shallowwave1D!(ηp1,ηn,ηm1,r)

  # simply rotate the variables, leaving the now-unused ηm1 array 
  # free to be modified as ηp1 in the next iteration
  (ηm1, ηn, ηp1) = (ηn, ηp1, ηm1)  
end

```

As already explained, this way your underlying arrays are only re-bound to other variables, but remain distinct (i.e. at any point in time, there are 3 arrays, independent from one another in the sense that modifying a coefficient in one of the array will not affect other arrays)

---

<div class="post-metadata">

### Author: ![natgeo-wong](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/natgeo-wong/32/10965_2.png) [@natgeo-wong](https://discourse.julialang.org/u/natgeo-wong)
#### Post date: [November 5, 2019, 4:49pm UTC](https://discourse.julialang.org/t/assignments-copies-and-deepcop/30723/12 "2019-11-05T16:49:24Z")

</div>

Mmmmmm interesting. Are there performance benefits to this, actually? Because I’m very new to the entire concept of mutable things, coming from MATLAB where all I did all day was reassign values to the variables haha.

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [November 5, 2019, 4:53pm UTC](https://discourse.julialang.org/t/assignments-copies-and-deepcop/30723/13 "2019-11-05T16:53:09Z")

</div>

> [@natgeo-wong](#):
>
> Are there performance benefits to this, actually?

Yes, often by _orders of magnitude_. The fact that Matlab insists on silently copying things all over the place is one of many design choices that make that language slow.

But don’t just take my word for it–use [GitHub - JuliaCI/BenchmarkTools.jl: A benchmarking framework for the Julia language](https://github.com/JuliaCI/BenchmarkTools.jl) to test for yourself, and feel free to ask follow up questions if you get surprising results.

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [November 5, 2019, 4:55pm UTC](https://discourse.julialang.org/t/assignments-copies-and-deepcop/30723/14 "2019-11-05T16:55:47Z")

</div>

For example, in [this post](https://discourse.julialang.org/t/improving-performance-of-a-nested-for-loop/29705/10) we were able to make the original user’s code over 3000 times faster by avoiding accidental copies and memory allocation.

---

<div class="post-metadata">

### Author: ![natgeo-wong](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/natgeo-wong/32/10965_2.png) [@natgeo-wong](https://discourse.julialang.org/u/natgeo-wong)
#### Post date: [November 5, 2019, 5:07pm UTC](https://discourse.julialang.org/t/assignments-copies-and-deepcop/30723/15 "2019-11-05T17:07:15Z")

</div>

ooooooooh okay thank you so much for the help I will go look at this again!
