# What's the best way to create a struct of arrays?

**URL:** <https://discourse.julialang.org/t/whats-the-best-way-to-create-a-struct-of-arrays/122238>\
**Category:** General Usage\
**Created:** [November 4, 2024, 3:44pm UTC](https://discourse.julialang.org/t/whats-the-best-way-to-create-a-struct-of-arrays/122238 "2024-11-04T15:44:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hssn15](https://avatars.discourse-cdn.com/v4/letter/h/8797f3/32.png) [@hssn15](https://discourse.julialang.org/u/hssn15)\
**Post date:** [November 4, 2024, 3:44pm UTC](https://discourse.julialang.org/t/whats-the-best-way-to-create-a-struct-of-arrays/122238/1 "2024-11-04T15:44:08Z")

</div>

I have another question which is about what is the best way to create structs.

```julia
struct s1{V1<:AbstractVector, V2<:AbstractVector{Int}}
    X1::V1
    X2::V1
    X3::V1
    State::V2
end

```

or

```julia
struct s2{T1}
    X1::Vector{T1}
    X2::Vector{T1}
    X3::Vector{T1}
    State::Vector{Int}
end

```

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [November 4, 2024, 4:58pm UTC](https://discourse.julialang.org/t/whats-the-best-way-to-create-a-struct-of-arrays/122238/2 "2024-11-04T16:58:51Z")

</div>

Hey @hssn15, I’ve split this secondary question out into its own topic.

Fundamentally, both will be able to perform exactly the same and have some similar behaviors, but which is best will depend on your use case:

- `s1` can support many array types (e.g., sparse, bitarray, etc., etc.). `s2` only supports `Vector`.
- `s2{T}` allows you to more easily introspect (and dispatch on) the element types of `X1`, `X2`, `X3`. `s1{V}` would allow more simple dispatch on the array types.

---

<div class="post-metadata">

**Author:** ![hssn15](https://avatars.discourse-cdn.com/v4/letter/h/8797f3/32.png) [@hssn15](https://discourse.julialang.org/u/hssn15)\
**Post date:** [November 4, 2024, 5:17pm UTC](https://discourse.julialang.org/t/whats-the-best-way-to-create-a-struct-of-arrays/122238/3 "2024-11-04T17:17:22Z")

</div>

Thank you for your reply. It was very helpful.

What about in terms of speed. In my case both can be used. But, I would like to choose faster version.

Thank You.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [November 4, 2024, 6:16pm UTC](https://discourse.julialang.org/t/whats-the-best-way-to-create-a-struct-of-arrays/122238/4 "2024-11-04T18:16:27Z")

</div>

> [@mbauman](#):
>
> both will be able to perform exactly the same

To be even more concrete, the performance (and even the memory layout) of `s1{Vector{Float64}, Vector{Int}}` will be **identical** to the performance of `s2{Float64}`.

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [November 4, 2024, 7:30pm UTC](https://discourse.julialang.org/t/whats-the-best-way-to-create-a-struct-of-arrays/122238/5 "2024-11-04T19:30:10Z")

</div>

You should check out StructArrays.jl, which stores things as a struct-of-arrays but also provides convenient access as if they were arrays-of-structs.
