# Whats the difference between a regular array and an array from StaticArrays

**URL:** <https://discourse.julialang.org/t/whats-the-difference-between-a-regular-array-and-an-array-from-staticarrays/14454>\
**Category:** General Usage\
**Created:** [September 3, 2018, 12:02am UTC](https://discourse.julialang.org/t/whats-the-difference-between-a-regular-array-and-an-array-from-staticarrays/14454 "2018-09-03T00:02:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![affans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/affans/32/11911_2.png) [@affans](https://discourse.julialang.org/u/affans)\
**Post date:** [September 3, 2018, 12:02am UTC](https://discourse.julialang.org/t/whats-the-difference-between-a-regular-array-and-an-array-from-staticarrays/14454/1 "2018-09-03T00:02:53Z")

</div>

Whats the difference between a regular array and an array from StaticArrays? From the docs, it seems that the size of array is known to the compiler based on the type, but isn’t this the same for a regular array as well, say `Array{Int64}(3)`?

Also, in what scenarios would we use either case? Up until now I’ve always used the regular built-in array but I am wondering if I can get a speed up using StaticArrays.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [September 3, 2018, 12:18am UTC](https://discourse.julialang.org/t/whats-the-difference-between-a-regular-array-and-an-array-from-staticarrays/14454/2 "2018-09-03T00:18:51Z")

</div>

The type of a regular Julia `Array` only includes the number of dimensions, not the size:

```julia
julia> typeof(Array{Int64}(3))
Array{Int64,1}

julia> typeof(Array{Int64}(10))
Array{Int64,1}

julia> typeof(Array{Int64}(1000000))
Array{Int64,1}

```

so the difference between a StaticArray and an Array is exactly what the docs here say: [https://github.com/JuliaArrays/StaticArrays.jl](https://github.com/JuliaArrays/StaticArrays.jl) , namely that the size of a StaticArray is actually part of the type:

```julia
julia> typeof(SVector(0, 0))
StaticArrays.SArray{Tuple{2},Int64,1,2}

julia> typeof(SVector(0, 0, 0))
StaticArrays.SArray{Tuple{3},Int64,1,3}

julia> typeof(SVector(0, 0, 0, 0))
StaticArrays.SArray{Tuple{4},Int64,1,4}

```

You would use StaticArrays when your program handles lots of small arrays (typically less than 100 elements) whose sizes are fixed or at least inferrable by the compiler. For example, if you’re storing points in 3D space, it will be much more efficient to use an `SVector{3, Float64}` than a regular `Array{Float64}` with 3 elements.

StaticArrays.jl also implements some optimized unrolled algorithms for small vectors and matrices. For example, StaticArrays.jl knows the closed-form of the matrix inverse for a 2x2 matrix:

```julia
julia> @btime inv($(rand(2, 2)))
  892.575 ns (10 allocations: 1.73 KiB)

julia> @btime inv($(rand(SMatrix{2, 2, Float64})))
  3.155 ns (0 allocations: 0 bytes)

```

but these optimizations are only really helpful for very small matrices. So a 2x2 or 3x3 or even 10x10 StaticArrays.SMatrix might make sense, but a 100x100 matrix is better left as a regular Julia Array.

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [September 3, 2018, 12:27am UTC](https://discourse.julialang.org/t/whats-the-difference-between-a-regular-array-and-an-array-from-staticarrays/14454/3 "2018-09-03T00:27:30Z")

</div>

To give an example of the optimized unrolled functions, let’s adapt the code from [StaticArrays.jl/linalg.jl at 1bc691df971d324d9e0e6c8f1aa2048163159f5b · JuliaArrays/StaticArrays.jl · GitHub](https://github.com/JuliaArrays/StaticArrays.jl/blob/1bc691df971d324d9e0e6c8f1aa2048163159f5b/src/linalg.jl#L225).

The code above uses `@generated`. This special macro will generate the function based on the types of the inputs allowing extreme specializations to happen for a specific input type. To actually see the body of the function generated, you can remove `@generated` as follows:

```julia
using StaticArrays

@inline mydot(a::StaticVector, b::StaticVector) = _vecdot(StaticArrays.same_size(a, b), a, b)
function _vecdot(::Size{S}, a::StaticArray, b::StaticArray) where {S}
    if prod(S) == 0
        return :(zero(promote_op(*, eltype(a), eltype(b))))
    end

    expr = :(conj(a[1]) * b[1])
    for j = 2:prod(S)
        expr = :($expr + conj(a[$j]) * b[$j])
    end

    return quote
        @_inline_meta
        @inbounds return $expr
    end
end

sv = SVector{3,Float64}([1.0,2.0,3.0]);
mydot(sv, sv)

```

This will give:

```julia
quote
    #= REPL[9]:12 =#
    #= REPL[9]:12 =# @_inline_meta
    #= REPL[9]:13 =#
    #= REPL[9]:13 =# @inbounds return (conj(a[1]) * b[1] + conj(a[2]) * b[2]) + conj(a[3]) * b[3]
end

```

So you see the “loop” that you would normally use to find the dot product of 2 vectors is now unrolled as an expression. This turns out to be faster when the expression is small enough because the loop adds a non-trivial overhead.

---

<div class="post-metadata">

**Author:** ![mbeach42](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbeach42/32/5747_2.png) [@mbeach42](https://discourse.julialang.org/u/mbeach42)\
**Post date:** [September 3, 2018, 1:20am UTC](https://discourse.julialang.org/t/whats-the-difference-between-a-regular-array-and-an-array-from-staticarrays/14454/4 "2018-09-03T01:20:27Z")

</div>

How can you declare StaticArrarys without declaring a global constant? For instance

```julia
struct MyType
    A::SVector{N, Float64}
    function MyType(x, y)
        A = x * rand(N) .+ y
    end
end

MyType(4)
MyType(6)

```

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [September 3, 2018, 5:29am UTC](https://discourse.julialang.org/t/whats-the-difference-between-a-regular-array-and-an-array-from-staticarrays/14454/5 "2018-09-03T05:29:37Z")

</div>

I think you need to read about parametric types [Types · The Julia Language](https://docs.julialang.org/en/v1/manual/types/index.html#Parametric-Types-1).

---

<div class="post-metadata">

**Author:** ![Juan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juan/32/7657_2.png) [@Juan](https://discourse.julialang.org/u/Juan)\
**Post date:** [November 22, 2018, 9:11pm UTC](https://discourse.julialang.org/t/whats-the-difference-between-a-regular-array-and-an-array-from-staticarrays/14454/6 "2018-11-22T21:11:50Z")

</div>

Why is better for large matrices to use regular Julia Array (with fixed sizes)?  
Do StaticArrays work well they contain Missings?

---

<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:** [November 22, 2018, 11:29pm UTC](https://discourse.julialang.org/t/whats-the-difference-between-a-regular-array-and-an-array-from-staticarrays/14454/7 "2018-11-22T23:29:19Z")

</div>

Please don’t necro old threads for barely related questions. Just open a new thread instead.

---

<div class="post-metadata">

**Author:** ![Juan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juan/32/7657_2.png) [@Juan](https://discourse.julialang.org/u/Juan)\
**Post date:** [November 22, 2018, 11:52pm UTC](https://discourse.julialang.org/t/whats-the-difference-between-a-regular-array-and-an-array-from-staticarrays/14454/8 "2018-11-22T23:52:17Z")

</div>

I’m sorry. The thread is only two months old and I’m asking about how the results posted by rdeits scale with size.
