# Matrices vs vectors of vectors performance

**URL:** <https://discourse.julialang.org/t/matrices-vs-vectors-of-vectors-performance/84157>\
**Category:** Numerics\
**Tags:** question, performance\
**Created:** [July 13, 2022, 10:07am UTC](https://discourse.julialang.org/t/matrices-vs-vectors-of-vectors-performance/84157 "2022-07-13T10:07:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dario-Rosa85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dario-rosa85/32/32358_2.png) [@Dario-Rosa85](https://discourse.julialang.org/u/Dario-Rosa85)\
**Post date:** [July 13, 2022, 10:07am UTC](https://discourse.julialang.org/t/matrices-vs-vectors-of-vectors-performance/84157/1 "2022-07-13T10:07:02Z")

</div>

Coming from Mathematica I have been used on thinking about matrices as lists of lists. Hence, I use the same point of view in Julia and, when I have many vectors to be treated homogeneously, I pack them into vectors of vectors. I am now wondering whether this is a wise choice. At the level of performance, is it more efficient to store collections of vectors as a vector of vectors or as a matrix (with each column being a vector)?

Thanks!

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [July 13, 2022, 10:30am UTC](https://discourse.julialang.org/t/matrices-vs-vectors-of-vectors-performance/84157/2 "2022-07-13T10:30:28Z")

</div>

Matrices of structs are stored contiguously in memory, whereas vectors of vectors aren’t. If you’re working with numerical data, it will generally be more efficient to use a matrix instead of a vector of vectors, if all the vectors have the same length.

---

<div class="post-metadata">

**Author:** ![Dario-Rosa85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dario-rosa85/32/32358_2.png) [@Dario-Rosa85](https://discourse.julialang.org/u/Dario-Rosa85)\
**Post date:** [July 13, 2022, 10:50am UTC](https://discourse.julialang.org/t/matrices-vs-vectors-of-vectors-performance/84157/3 "2022-07-13T10:50:28Z")

</div>

thanks. it was the kind of answer I was looking for. I will give it a try and compare

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [July 13, 2022, 12:35pm UTC](https://discourse.julialang.org/t/matrices-vs-vectors-of-vectors-performance/84157/4 "2022-07-13T12:35:32Z")

</div>

If the inner vectors have a small length, you may use StaticArrays.jl to represent them. Then your vector of SVectors will have the same layout in memory as a matrix.
