Matrices vs vectors of vectors performance

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!

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.

3 Likes

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

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.

3 Likes