Statically sized arrays for Julia
StaticArrays provides a framework for implementing statically sized arrays in Julia, using the abstract type StaticArray{Size,T,N} <: AbstractArray{T,N}
. Subtypes of StaticArray
will provide fast implementations of common array and linear algebra operations. Note that here "statically sized" means that the size can be determined from the type, and "static" does not necessarily imply immutable
.
The package also provides some concrete static array types: SVector
, SMatrix
and SArray
, which may be used as-is (or else embedded in your own type). Mutable versions MVector
, MMatrix
and MArray
are also exported, as well as SizedArray
for annotating standard Array
s with static size information. Further, the abstract FieldVector
can be used to make fast static vectors out of any uniform Julia "struct".
When a program uses many small ($\lesssim 100$ elements) fixed-sized arrays (whose size is known by the compiler, i.e. "statically," when the performance-critical code is compiled), then using Static Arrays can have several performance advantages:
v3 = v1 + v2
is just implemented as 3 scalar additions, with no loop overhead at all, if these are all SVector{3, Float64}
. The unrolled loops can also sometimes trigger SIMD optimizations.Vector{SVector{3, Float64}}
, such as [v1, v2, v3]
from the SVector
s in the previous example, is stored as $3N$ consecutive Float64
values. This is much more efficient to access than a Vector{Vector{Float64}}
, which is essentially an array of pointers to Vector{Float64}
arrays, and is often faster and more convenient than a $3 \times N$ Matrix{Float64}
(e.g. because the length 3 is stored in the type you get the benefits of (1) and (2) when accessing vectors in the array, and no "slicing" or "views" are needed). (There is also HybridArrays.jl for matrices with a static number of rows.)You probably don't want to use Static Arrays if:
Matrix
, whereas you wouldn't want to store a large array inline in an arbitrary struct
.Array
(because the size of the former is a constant encoded in the type), and it may not be worth the inconvenience for cases where performance is not paramount.RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4