# Vector of Vectors / Vector of Tuples

**URL:** <https://discourse.julialang.org/t/vector-of-vectors-vector-of-tuples/123368>\
**Category:** Performance\
**Tags:** question\
**Created:** [December 2, 2024, 6:16pm UTC](https://discourse.julialang.org/t/vector-of-vectors-vector-of-tuples/123368 "2024-12-02T18:16:54Z")\
**Posts on this page:** 7\
**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:** [December 2, 2024, 6:16pm UTC](https://discourse.julialang.org/t/vector-of-vectors-vector-of-tuples/123368/1 "2024-12-02T18:16:54Z")

</div>

I am making a simulator and need to store data in pre-allocated storages. But, I am a bit confused and worried about the performance. I have two ways to create storage element.

First way:

```julia
storage = [
            (x1, y1, z1), 
            (x2, y2, z2), 
            (x3, y3, z3),
             ..........
            (xn, yn, zn)
          ]

```

Second way:

```julia
storage = [ 
            [x1, x2, x3, ... xn],
            [y1, y2, y3, ... yn],
            [z1, z2, z3, ... zn],
          ]

```

So, would love to get some advices about which one has more potential to be much faster.

Thank You.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [December 2, 2024, 6:19pm UTC](https://discourse.julialang.org/t/vector-of-vectors-vector-of-tuples/123368/2 "2024-12-02T18:19:11Z")

</div>

Probably Tuples, as those might be stored inline, though that heavily depends on what you’re putting in those Tuples. You’ll want to check if `isbitstype(eltype(storage))` is `true`. Mutable Vectors on the other hand must be allocated elsewhere (`isbitstype` will return `false`), and you lose out on some CPU optimizations the more you have to jump around in virtual memory. Depending on how you’re accessing the data, you could also benefit from splitting fields into separate arrays (look up Struct of Arrays vs Array of Structs, and there are a few Julia libraries to help with that but I’m not familiar).

---

<div class="post-metadata">

**Author:** ![Paulo\_Jabardo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paulo_jabardo/32/3196_2.png) [@Paulo\_Jabardo](https://discourse.julialang.org/u/Paulo_Jabardo)\
**Post date:** [December 3, 2024, 9:42am UTC](https://discourse.julialang.org/t/vector-of-vectors-vector-of-tuples/123368/3 "2024-12-03T09:42:22Z")

</div>

This is the old structure of arrays problem. Package [`StrucArrays.jl`](https://juliaarrays.github.io/StructArrays.jl/stable/) is also another possibility where you store the array the second way and access each individual point in the first way.

---

<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:** [December 3, 2024, 2:38pm UTC](https://discourse.julialang.org/t/vector-of-vectors-vector-of-tuples/123368/4 "2024-12-03T14:38:17Z")

</div>

It sounds like people’s above suggestions for StructArrays.jl might serve you well. But in case you’re after an actual vector of _vectors_ (rather than mere collections of values, where structs or tuples are more flexible) and those inner vectors are of small (\<100 elements) and constant size, consider using `SVector` from StaticArrays.jl. `SVector` is just a `NTuple` wrapped to behave like a (immutable) vector. This means it’s just as fast as a `NTuple` or struct but you get all the linear algebra of an `AbstractVector`.

---

<div class="post-metadata">

**Author:** ![simonschoelly](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonschoelly/32/21606_2.png) [@simonschoelly](https://discourse.julialang.org/u/simonschoelly)\
**Post date:** [December 3, 2024, 3:11pm UTC](https://discourse.julialang.org/t/vector-of-vectors-vector-of-tuples/123368/5 "2024-12-03T15:11:50Z")

</div>

I assume that each data point has size `3` and `x`, `y`, and `z` have the same types, otherwise there are more things to consider.

It also depends how you access your data. If you access `x_i`, `y_i`, and `z_i` always at the same time, then the first solution will be more performant, as the values can be fetched together from memory and will also be cached together.

Another solutions that have not been mentioned yet is to use a `Matrix `of dimension `(3, n)` or `(n, 3)`. Julia uses [column-major order](https://en.wikipedia.org/wiki/Row-_and_column-major_order), therefore the dimension `(3, n)` is similar to your first solution and `(n, 3)` is similar to your second solution.

And one option that should be mentioned for completeness - but is unlikely to fit your case - is using a `DataFrame` from [DataFrames.j](https://github.com/JuliaData/DataFrames.jl). A `DataFrame` is collection of columns therefore this would also resemble your second solution.

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [December 3, 2024, 3:38pm UTC](https://discourse.julialang.org/t/vector-of-vectors-vector-of-tuples/123368/6 "2024-12-03T15:38:42Z")

</div>

My tip would be to write the code first, in the way that makes it cleanest and easiest to read and write. Then worry about the most efficient storage later.

---

<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:** [December 3, 2024, 4:27pm UTC](https://discourse.julialang.org/t/vector-of-vectors-vector-of-tuples/123368/7 "2024-12-03T16:27:49Z")

</div>

> [@hssn15](#):
>
> ```julia
> storage = [
> (x1, y1, z1), 
> (x2, y2, z2), 
> 
> ```

If these are (x,y,z) values representing a point in 3D, that you will be doing lots of 3-component vector calculations with, you should really consider an array of 3-component `SVector`s from StaticArrays.jl, since then not only will the storage be more efficient but also the 3-vector calculations will also be much quicker.

This just further illustrates @simonschoelly’s point that the best choice of datastructure depends on what you want to _do_ with the data. You haven’t told us, so we are all just speculating.
