# Broadcasting slower than for-loop

**URL:** <https://discourse.julialang.org/t/broadcasting-slower-than-for-loop/107562>\
**Category:** New to Julia\
**Created:** [December 13, 2023, 11:07am UTC](https://discourse.julialang.org/t/broadcasting-slower-than-for-loop/107562 "2023-12-13T11:07:21Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [December 13, 2023, 1:17pm UTC](https://discourse.julialang.org/t/broadcasting-slower-than-for-loop/107562/5 "2023-12-13T13:17:56Z")

</div>

Variables in julia are just labels for objects. The variables can always be changed, but that doesn’t change the underlying data. Nothing is being mutated.

When you write

```julia
y = y .^ 2

```

you’re saying “I want to take my label `y` and change it so that it now labels the result of `y .^ 2`.” The operation `y .^ 2` allocates a whole new vector. The data `y` used to label is left unchanged.

For example, let’s make a vector `y` and then say `z = y`, and then re-bind `y`. What ends up happening?

```julia-repl
julia> let y = [1, 2, 3]
           z = y
           y = [4, 5, 6]
           z
       end
3-element Vector{Int64}:
 1
 2
 3

```

At the end of this, `z` is no longer labelleing the same data as `y`.

Mutation is different. When you mutate something, you’re changing the actual data that the label is pointing to. E.g.:

```julia-repl
julia> let y = [1, 2, 3]
           z = y
           y .= [4, 5, 6]
           z
       end
3-element Vector{Int64}:
 4
 5
 6

```

In this case, `z` and `y` are still labelling the same data, but have _both_ been changed by the mutation.

---

_[View the full topic](https://discourse.julialang.org/t/broadcasting-slower-than-for-loop/107562)._
