# Slowdown with StaticArrays and broadcasting

**URL:** <https://discourse.julialang.org/t/slowdown-with-staticarrays-and-broadcasting/16410>\
**Category:** General Usage\
**Tags:** broadcast, array\
**Created:** [October 16, 2018, 3:04pm UTC](https://discourse.julialang.org/t/slowdown-with-staticarrays-and-broadcasting/16410 "2018-10-16T15:04:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielmatz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielmatz/32/2285_2.png) [@danielmatz](https://discourse.julialang.org/u/danielmatz)\
**Post date:** [October 16, 2018, 3:04pm UTC](https://discourse.julialang.org/t/slowdown-with-staticarrays-and-broadcasting/16410/1 "2018-10-16T15:04:41Z")

</div>

I’ve been trying to use the master branch of StaticArrays and Julia 1.0.1. My code works fine with the normal `Array`s, but when I use `SArray`s, there’s a noticeable slowdown.

Are there still some open issues with StaticArrays on 1.0? Particularly regarding broadcasting?

Here’s my attempt at a minimal example.

```julia
using StaticArrays
using BenchmarkTools

function foo(x, dt, f1, f2, f3, f4, f5, f6, f7)
    b1 = 0.09646076681806523
    b2 = 0.01
    b3 = 0.4798896504144996
    b4 = 1.379008574103742
    b5 = -3.290069515436081
    b6 = 2.324710524099774
    b7 = 0

    xn = @. x + dt * (b1*f1 + b2*f2 + b3*f3 + b4*f4 + b5*f5 + b6*f6 + b7*f7)

    xn
end

x = @SVector(rand(3))
f1 = @SVector(rand(3))
f2 = @SVector(rand(3))
f3 = @SVector(rand(3))
f4 = @SVector(rand(3))
f5 = @SVector(rand(3))
f6 = @SVector(rand(3))
f7 = @SVector(rand(3))
@btime foo($x, 1e-5, $f1, $f2, $f3, $f4, $f5, $f6, $f7)

```

Without broadcasting (removing the `@.`), I get:

```julia
  15.837 ns (0 allocations: 0 bytes)

```

With broadcasting, I get:

```julia
  14.477 μs (427 allocations: 21.33 KiB)

```

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [October 16, 2018, 3:58pm UTC](https://discourse.julialang.org/t/slowdown-with-staticarrays-and-broadcasting/16410/2 "2018-10-16T15:58:34Z")

</div>

It seems some inference limit is getting reached,

```julia
   xn = x .+ dt * (b1*f1 .+ b2*f2 .+ b3*f3 .+ b4*f4)

```

works ok but

```julia
 xn = x .+ dt * (b1*f1 .+ b2*f2 .+ b3*f3 .+ b4*f4 .+ b5*f5)

```

allocates

---

<div class="post-metadata">

**Author:** ![danielmatz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielmatz/32/2285_2.png) [@danielmatz](https://discourse.julialang.org/u/danielmatz)\
**Post date:** [October 16, 2018, 4:24pm UTC](https://discourse.julialang.org/t/slowdown-with-staticarrays-and-broadcasting/16410/3 "2018-10-16T16:24:46Z")

</div>

Is there anything I can do about it? Or is there something StaticArrays can do about it?

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [October 16, 2018, 8:48pm UTC](https://discourse.julialang.org/t/slowdown-with-staticarrays-and-broadcasting/16410/4 "2018-10-16T20:48:08Z")

</div>

> [@danielmatz](#):
>
> Is there anything I can do about it?

Just remove the `@.`?

---

<div class="post-metadata">

**Author:** ![danielmatz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielmatz/32/2285_2.png) [@danielmatz](https://discourse.julialang.org/u/danielmatz)\
**Post date:** [October 16, 2018, 8:51pm UTC](https://discourse.julialang.org/t/slowdown-with-staticarrays-and-broadcasting/16410/5 "2018-10-16T20:51:08Z")

</div>

Sure, but I was hoping that this particular bit of code could be used for any `AbstractVector`… So in the general case I think I need to use broadcasting.

---

<div class="post-metadata">

**Author:** ![danielmatz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielmatz/32/2285_2.png) [@danielmatz](https://discourse.julialang.org/u/danielmatz)\
**Post date:** [October 17, 2018, 8:55pm UTC](https://discourse.julialang.org/t/slowdown-with-staticarrays-and-broadcasting/16410/6 "2018-10-17T20:55:56Z")

</div>

In addition to the difference in run time, there also seems to be something going on with StaticArrays and broadcasting causing large compilation times. It is somewhat noticeable with the example above. The function that I really notice it with has about 10 broadcasted equations of the size above.
