# Array of Structs vs Structs of Arrays

**URL:** <https://discourse.julialang.org/t/array-of-structs-vs-structs-of-arrays/122227>\
**Category:** Performance\
**Created:** [November 4, 2024, 2:48pm UTC](https://discourse.julialang.org/t/array-of-structs-vs-structs-of-arrays/122227 "2024-11-04T14:48:16Z")\
**Posts on this page:** 3\
**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, 2:48pm UTC](https://discourse.julialang.org/t/array-of-structs-vs-structs-of-arrays/122227/1 "2024-11-04T14:48:16Z")

</div>

Hi, I have a question about data storing.

I am developing numerical simulator and need to store variables (temporarily) for each iterations. Those variables change in each iterations.

But I am not sure what kind of storing method I should use to have faster processing.

(If I use Array of Structs, Sturcts should be mutable type of structs)

I would love to get some advices about that.

Thank you.

---

<div class="post-metadata">

**Author:** ![Salmon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/salmon/32/22968_2.png) [@Salmon](https://discourse.julialang.org/u/Salmon)\
**Post date:** [November 4, 2024, 3:16pm UTC](https://discourse.julialang.org/t/array-of-structs-vs-structs-of-arrays/122227/2 "2024-11-04T15:16:58Z")

</div>

AFAIK, for most cases, a struct of arrays is the most performant, because it will be easier for the compiler to do SIMD.  
While one sometimes has arrays of structs as the result of some calculation, it can be useful to convert this datastructure. For this there is the package [StructArrays](https://juliaarrays.github.io/StructArrays.jl/stable/)

> [@hssn15](#):
>
> (If I use Array of Structs, Sturcts should be mutable type of structs)

Note that if you have an array of mutable structs, this is very likely to be slow in particular, because it might be that each element of this array will live at a different location in memory.

---

<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:53pm UTC](https://discourse.julialang.org/t/array-of-structs-vs-structs-of-arrays/122227/3 "2024-11-04T16:53:56Z")

</div>

A post was split to a new topic: [What’s the best way to create a struct of arrays?](https://discourse.julialang.org/t/whats-the-best-way-to-create-a-struct-of-arrays/122238)
