# Switching between StaticArray and regular array depending on size (?)

**URL:** <https://discourse.julialang.org/t/switching-between-staticarray-and-regular-array-depending-on-size/93437>\
**Category:** Performance\
**Tags:** staticarrays\
**Created:** [January 24, 2023, 1:53am UTC](https://discourse.julialang.org/t/switching-between-staticarray-and-regular-array-depending-on-size/93437 "2023-01-24T01:53:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![baptiste](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baptiste/32/9813_2.png) [@baptiste](https://discourse.julialang.org/u/baptiste)\
**Post date:** [January 24, 2023, 1:53am UTC](https://discourse.julialang.org/t/switching-between-staticarray-and-regular-array-depending-on-size/93437/1 "2023-01-24T01:53:40Z")

</div>

I’m developing a package for physics simulations, where the core routine solves a dense linear system of complex equations,

\mathbf{A} \mathbf{X} = \mathbf{B}

The matrix \mathbf{A} can be small in some applications, for example 6\times6, but other use cases at the other extreme would be more like 10,000\times10,000. I’m wondering if it makes sense to have a switch in the high-level (user-facing) routine depending on the size of the problem,

```julia

function simulation(N)

  # using random numbers as dummy calculation
    if N > 100
        A = rand(Complex{Float64}, (N,N))
        Y = rand(Complex{Float64}, (N, 5))
        # A = Matrix{Complex{Float64}}(undef, N, N)
        # Y = Matrix{Complex{Float64}}(undef, N, 5)
        @info "using regular Array"
    elseif N <= 100
        A = rand(SMatrix{N,N,Complex{Float64},N^2})
        Y = rand(SMatrix{N,5,Complex{Float64},5N})
        @info "using static Array"
    end

    X = A \ Y
    # [...] more calculations with X and Y in the real-world situation
    return abs2.(X)
end

simulation(3)
simulation(300)

```

Does this make sense? Is there something already in StaticArrays to take care of this sort of situation?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [January 24, 2023, 1:59am UTC](https://discourse.julialang.org/t/switching-between-staticarray-and-regular-array-depending-on-size/93437/2 "2023-01-24T01:59:50Z")

</div>

this naive implementation will have type instability, so be sure to do it in a separate function. but theoretically it makes sense, if you use the small matrix enough times it can be worth while

---

<div class="post-metadata">

**Author:** ![baptiste](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baptiste/32/9813_2.png) [@baptiste](https://discourse.julialang.org/u/baptiste)\
**Post date:** [January 24, 2023, 2:22am UTC](https://discourse.julialang.org/t/switching-between-staticarray-and-regular-array-depending-on-size/93437/3 "2023-01-24T02:22:04Z")

</div>

Can you please clarify what part(s) I should do in a separate function? At some point there will have to be a if(N\<100) returning either Array or SArray, depending on N. The matrix is typically used a hundred times, so it seems worth contemplating, especially when size ~ 3x3 etc. where solve() has special cases.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 24, 2023, 3:10am UTC](https://discourse.julialang.org/t/switching-between-staticarray-and-regular-array-depending-on-size/93437/4 "2023-01-24T03:10:25Z")

</div>

> [@baptiste](#):
>
> The matrix is typically used a hundred times, so it seems worth contemplating, especially when

This doesn’t seem worth optimizing for small matrices — you can solve 100 6x6 systems in a few microseconds, so why do you care how fast it is? If you are solving only 100 times, only the bigger matrix sizes will matter.

---

<div class="post-metadata">

**Author:** ![baptiste](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baptiste/32/9813_2.png) [@baptiste](https://discourse.julialang.org/u/baptiste)\
**Post date:** [January 24, 2023, 3:20am UTC](https://discourse.julialang.org/t/switching-between-staticarray-and-regular-array-depending-on-size/93437/5 "2023-01-24T03:20:25Z")

</div>

The matrix is used a few hundred times in this function, but the function itself might be called 1000s of times by the user. Granted it’s still not that long to run, but I’d like to see if there’s a good idiomatic way to deal with this kind of situation, if just for my own learning.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 24, 2023, 3:49am UTC](https://discourse.julialang.org/t/switching-between-staticarray-and-regular-array-depending-on-size/93437/6 "2023-01-24T03:49:55Z")

</div>

Basically, there are two cases: (1) you’re calling it enough times that the costs of compiling a specialized version for a specific matrix size (a StaticArray) and dispatching to it dynamically are worth it, or (2) you aren’t. Since even calling it 100000 times probably takes less than a second for a 6x6 matrix, you may be in case (2).
