# In place updating of arrays

**URL:** <https://discourse.julialang.org/t/in-place-updating-of-arrays/55983>\
**Category:** General Usage\
**Created:** [February 25, 2021, 6:32am UTC](https://discourse.julialang.org/t/in-place-updating-of-arrays/55983 "2021-02-25T06:32:27Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![praveenc](https://avatars.discourse-cdn.com/v4/letter/p/0ea827/32.png) [@praveenc](https://discourse.julialang.org/u/praveenc)\
**Post date:** [February 25, 2021, 6:32am UTC](https://discourse.julialang.org/t/in-place-updating-of-arrays/55983/1 "2021-02-25T06:32:27Z")

</div>

If we have array equation like

`x = x + y`

this creates extra memory allocation. Does the following do in place update

`x .= x + y`

How to find out if the update is happening in place ?

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [February 25, 2021, 6:38am UTC](https://discourse.julialang.org/t/in-place-updating-of-arrays/55983/3 "2021-02-25T06:38:57Z")

</div>

Can’t answer the second question, but that case is what `x .+= y` is for.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [February 25, 2021, 6:43am UTC](https://discourse.julialang.org/t/in-place-updating-of-arrays/55983/4 "2021-02-25T06:43:43Z")

</div>

The best way to check if this is happening in place is `@allocated` (or `@time`).

---

<div class="post-metadata">

**Author:** ![Satvik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/satvik/32/20486_2.png) [@Satvik](https://discourse.julialang.org/u/Satvik)\
**Post date:** [February 25, 2021, 6:44am UTC](https://discourse.julialang.org/t/in-place-updating-of-arrays/55983/5 "2021-02-25T06:44:49Z")

</div>

I had a surprisingly difficult time running tests on this, because Julia seems to be quite smart about optimizing it. For example:

```julia
julia> a = rand(100);

julia> b = rand(100);

julia> c = zeros(100);

julia> using BenchmarkTools

julia> @btime c = a + b;
  73.847 ns (1 allocation: 896 bytes)

```

The fact that it does just one allocation suggests to me that julia is already doing `c = a + b` in place, even without `.=`, although maybe I’m missing something.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [February 25, 2021, 6:47am UTC](https://discourse.julialang.org/t/in-place-updating-of-arrays/55983/6 "2021-02-25T06:47:12Z")

</div>

That’s weird. That said, in general, doing these tests in the global scope will often lead you astray. What happens if you try `f!(c,a,b) = (c = a+b)`?

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [February 25, 2021, 6:48am UTC](https://discourse.julialang.org/t/in-place-updating-of-arrays/55983/7 "2021-02-25T06:48:15Z")

</div>

It is indeed clever about that, but your case is not the same as in the question. It’s `x = x + y`, not `z = x + y`

---

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [February 25, 2021, 6:49am UTC](https://discourse.julialang.org/t/in-place-updating-of-arrays/55983/8 "2021-02-25T06:49:20Z")

</div>

Julia is doing everything correctly. Operation `a + b` requires 1 allocation to create a new array and `c = ...` binds variable `c` to this new array. Binding does not require allocation.

In other words, `c = a + b` is roughly equivalent to

```julia
temp = Vector{Float64}(undef, 100)
for i in 1:100
   temp[i] = a[i] + b[i]
end
c = temp

```

If you want inplace operation, you need to use `.` both in `+` and `=`. Then this operations are fused together and turns into inplace update. I.e. `c .= a .+ b` is equivalent to

```julia
for i in 1:100
  c[i] = a[i] + b[i]
end

```

UPD: operation `c .= a + b` is rather meaningless, because it is equivalent to

```julia
temp = Vector{Float64}(undef, 100)
for i in 1:100
   temp[i] = a[i] + b[i]
end

for i in 1:100
  c[i] = temp[i]
end

```

so it run even slower than `c = a + b` because instead of binding variable, it copies data.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [February 25, 2021, 7:13am UTC](https://discourse.julialang.org/t/in-place-updating-of-arrays/55983/10 "2021-02-25T07:13:47Z")

</div>

You are benchmarking with global variables, so the allocations are going crazy (you should be seeing zero or 1 or 2 allocations, not hundreds of thousands).

Use BenchmarkTools.jl, don’t use the `@time` macro for micro-benchmarks, and avoid global variables.

---

<div class="post-metadata">

**Author:** ![praveenc](https://avatars.discourse-cdn.com/v4/letter/p/0ea827/32.png) [@praveenc](https://discourse.julialang.org/u/praveenc)\
**Post date:** [February 25, 2021, 7:37am UTC](https://discourse.julialang.org/t/in-place-updating-of-arrays/55983/11 "2021-02-25T07:37:50Z")

</div>

Thanks, I deleted my post so as not to mislead future readers.
