# When to use StaticArrays

**URL:** <https://discourse.julialang.org/t/when-to-use-staticarrays/46123>\
**Category:** Performance\
**Created:** [September 5, 2020, 10:34pm UTC](https://discourse.julialang.org/t/when-to-use-staticarrays/46123 "2020-09-05T22:34:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielw2904](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielw2904/32/10890_2.png) [@danielw2904](https://discourse.julialang.org/u/danielw2904)\
**Post date:** [September 5, 2020, 10:34pm UTC](https://discourse.julialang.org/t/when-to-use-staticarrays/46123/1 "2020-09-05T22:34:56Z")

</div>

I have been looking into `StaticArrays.jl` to speed up linear algebra but I think I am doing something wrong or testing the wrong things.

My first test was a simple linear model:

```julia
using StaticArrays, Random, BenchmarkTools
Random.seed!(123);
N = 100
p = 10
βtrue = 10 .* rand(p);
_X = rand(N, p);
_y = _X * βtrue + randn(N);

X = SMatrix{N, p}(_X);
y = SVector{N}(_y);
βstore = MVector{p}(zeros(p));

function lm!(X, y, b)
    b[:] = X\y
end

@btime lm!(X, y, βstore)

_βstore = zeros(p);
@btime lm!(_X, _y, _βstore)

```

But it looks like the version with regular arrays leads to fewer allocations and is faster:

```julia
julia> @btime lm!(X, y, βstore);
  11.318 μs (87 allocations: 94.55 KiB)

julia> @btime lm!(_X, _y, _βstore);
  10.734 μs (85 allocations: 86.52 KiB)

```

So my question is when to use StaticArrays and whether I am doing something wrong here?

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [September 5, 2020, 10:54pm UTC](https://discourse.julialang.org/t/when-to-use-staticarrays/46123/2 "2020-09-05T22:54:56Z")

</div>

The README of StaticArrays.jl says:  
A very rough rule of thumb is that you should consider using a normal `Array` for arrays larger than 100 elements.

Your array X has already 1000 elements.

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [September 5, 2020, 11:02pm UTC](https://discourse.julialang.org/t/when-to-use-staticarrays/46123/3 "2020-09-05T23:02:54Z")

</div>

Note that `b[:] = X\y` will still allocate, you probably want to use [`lmul!`](https://docs.julialang.org/en/v1/stdlib/LinearAlgebra/#LinearAlgebra.ldiv!) here instead. For `StaticArray`s there’s also really no need to write this as a mutating function, you can just return `X\y`. Of course @ufechner7’s point still applies, I don’t expect any benefit from using StaticArrays for arrays this large.

---

<div class="post-metadata">

**Author:** ![danielw2904](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielw2904/32/10890_2.png) [@danielw2904](https://discourse.julialang.org/u/danielw2904)\
**Post date:** [September 5, 2020, 11:09pm UTC](https://discourse.julialang.org/t/when-to-use-staticarrays/46123/4 "2020-09-05T23:09:38Z")

</div>

Thanks! I misread that as just applying to compile time. I get the best performance if I use `MVector` only for the parameters since they are quite low dimensional.

```julia
julia> @btime lm!(_X, _y, βstore);
  10.336 μs (85 allocations: 86.52 KiB)

julia> @btime lm!(_X, _y, _βstore);
  11.391 μs (85 allocations: 86.52 KiB)

```

---

<div class="post-metadata">

**Author:** ![danielw2904](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielw2904/32/10890_2.png) [@danielw2904](https://discourse.julialang.org/u/danielw2904)\
**Post date:** [September 5, 2020, 11:11pm UTC](https://discourse.julialang.org/t/when-to-use-staticarrays/46123/5 "2020-09-05T23:11:31Z")

</div>

I eventually plan to use this in MCMC so I’m overwriting the parameters quite often. This was just meant as a minimal example 🙂 I’ll test with some more realistic examples. Thanks for the help!
