To motivate my question, consider the case when dealing with jagged arrays (for simplicity's sake) of element type Int
in Julia. There are two ways to store them:
- As
Vector{Vector{Int}}
- As
Vector{Union{Vector{Int}, Int}}
(especially, if one expects to store a sufficiently large number of 1-element vectors)
My question is which one is more efficient / faster / better?
To answer it, among the other things, I need to know how each is stored in memory. Namely:
I presume that variable of a type
Vector{Vector{Int}}
, would be considered homogeneous type array, and therefore I would expect it to be stored contiguously in memory, and as such to be more cpu-cache-friendly. Am I right? Or contiguity only applies to arrays whose elements' data type is primitive?Would variable of a type
Vector{Union{Vector{Int}, Int}}
considered heterogeneous array, and as such stored not contiguously in memory?How benefit of contiguous representation in memory is compared to the benefit of not having array container for 1-element arrays members, i.e. storing them as primitive data type (
Int
in this case)? Which one yields more efficiency?