# Why does broadcast operation allocate?

**URL:** <https://discourse.julialang.org/t/why-does-broadcast-operation-allocate/65428>\
**Category:** Performance\
**Created:** [July 28, 2021, 10:58am UTC](https://discourse.julialang.org/t/why-does-broadcast-operation-allocate/65428 "2021-07-28T10:58:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnomotani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnomotani/32/26753_2.png) [@johnomotani](https://discourse.julialang.org/u/johnomotani)\
**Post date:** [July 28, 2021, 10:58am UTC](https://discourse.julialang.org/t/why-does-broadcast-operation-allocate/65428/1 "2021-07-28T10:58:52Z")

</div>

Why does this broadcasting allocate?

```julia
julia> a = rand(241); b = rand(241);

julia> @time a .-= b;
  0.111315 seconds (188.85 k allocations: 11.037 MiB, 99.59% compilation time)

julia> @time a .-= b;
  0.000016 seconds (2 allocations: 64 bytes)

```

I know it’s only 64 bytes, but in my actual code, the broadcast is inside a loop… For comparison, if I do the same operation using a loop there is no allocation at all

```julia
julia> a = rand(241); b = rand(241);

julia> function minuseq!(a,b)
       for i in eachindex(a)
       a[i] -= b[i]
       end
       end
minuseq! (generic function with 1 method)

julia> @time minuseq!(a,b);
  0.016635 seconds (4.13 k allocations: 229.832 KiB, 99.81% compilation time)

julia> @time minuseq!(a,b);
  0.000005 seconds

```

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [July 28, 2021, 11:01am UTC](https://discourse.julialang.org/t/why-does-broadcast-operation-allocate/65428/2 "2021-07-28T11:01:47Z")

</div>

It doesn’t:

```julia
julia> a = rand(241); b = rand(241);

julia> function minuseq!(a,b)
           a .-= b
           nothing
       end
minuseq! (generic function with 1 method)

julia> @time minuseq!(a,b)
  0.046553 seconds (196.53 k allocations: 11.362 MiB, 99.96% compilation time)

julia> @time minuseq!(a,b)
  0.000003 seconds

```

---

<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:** [July 28, 2021, 11:05am UTC](https://discourse.julialang.org/t/why-does-broadcast-operation-allocate/65428/3 "2021-07-28T11:05:38Z")

</div>

The allocations come from using non-constant globals when `a .-= b` is evaluated in the global scope.

When you benchmark a function, there is no need for allocation since everything is type-stable.

---

<div class="post-metadata">

**Author:** ![johnomotani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnomotani/32/26753_2.png) [@johnomotani](https://discourse.julialang.org/u/johnomotani)\
**Post date:** [July 28, 2021, 11:06am UTC](https://discourse.julialang.org/t/why-does-broadcast-operation-allocate/65428/4 "2021-07-28T11:06:21Z")

</div>

Thanks @pfitzseb. I know to be more careful testing things in the REPL now. Now need to find an actual MWE for the allocations during broadcasting in my code…
