# Are StaticArrays always faster?

**URL:** <https://discourse.julialang.org/t/are-staticarrays-always-faster/108225>\
**Category:** Performance\
**Tags:** matrices, staticarrays\
**Created:** [January 1, 2024, 9:27am UTC](https://discourse.julialang.org/t/are-staticarrays-always-faster/108225 "2024-01-01T09:27:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Uranium238](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uranium238/32/209706_2.png) [@Uranium238](https://discourse.julialang.org/u/Uranium238)\
**Post date:** [January 1, 2024, 9:27am UTC](https://discourse.julialang.org/t/are-staticarrays-always-faster/108225/1 "2024-01-01T09:27:17Z")

</div>

I was under the impression that `StaticArrays` or operations that involve `SMatrix` are faster than the non `Static` counterparts. To test this out I wrote a small code that does matrix-multiplication related computations of a `Matrix` and it’s `SMatrix` equivalent. The code is given below

```julia
using BenchmarkTools
using StaticArrays
function G_dynamic(M::Matrix{Float64})
    p = M' * M;
    q = p^2 ;
    r =p^-1;
end
function G_static(M::SMatrix)
    p = M' * M
    q = p^2 
    r =p^-1
end
M = rand(100,100)
M_static = SMatrix{100,100}(M)

@btime G_dynamic(M)

@btime G_static(M_static)

```

But to my surprise the `G_static` takes **much** more time than the non static counterpart. Any ideas why this is happening or under what circumstances are `StaticArrays` faster ?

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [January 1, 2024, 9:53am UTC](https://discourse.julialang.org/t/are-staticarrays-always-faster/108225/2 "2024-01-01T09:53:44Z")

</div>

From the README:

> Note that in the current implementation, working with large `StaticArray` s puts a lot of stress on the compiler, and becomes slower than `Base.Array` as the size increases. A very rough rule of thumb is that you should consider using a normal `Array` for arrays larger than 100 elements.

100x100 is indeed larger than 100. Incidentally, write `@btime G_dynamic($M)` instead because `M` is an untyped global and that will spuriously introduce type instability to the benchmark loop that the normal function call won’t have past its initial dispatch.

---

<div class="post-metadata">

**Author:** ![simsurace](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simsurace/32/30216_2.png) [@simsurace](https://discourse.julialang.org/u/simsurace)\
**Post date:** [January 1, 2024, 10:07am UTC](https://discourse.julialang.org/t/are-staticarrays-always-faster/108225/3 "2024-01-01T10:07:31Z")

</div>

I agree that 100x100 is no longer small in the context of StaticArrays.

Another point which has nothing to do with performance, but Julia‘s programming model, is that since your code is the same for the two matrix types, you could write a generic function for argument type `AbstractMatrix` and it would still be compiled to two different specialized methods for the two types you call it on, with no performance penalty. This is a great strength of Julia.
