Preallocating Vector of Vectors

Using the original OP’s:

function makearr2(T, Nrow, Ncol)
    return [Vector{T}(undef, Nrow) for _ in 1:Ncol]

I think the most idiomatic would be ::Type{T}, though it probably doesn’t make any performance difference.

1 Like

I have the suspicion this is variation of our recent discussion How to do custom allocators. ArrayOfArrays.jl probably uses some custom allocators, too.

If your vectors all have the same size, you could try zeros(m,n) and then go with
Zero-Cost Abstractions in Julia: Indexing Vectors by Name with LabelledArrays - Stochastic Lifestyle

1 Like

This is a really good tip with push!, and I would not have expected that if I hadnt seen that. Thank you!

1 Like

I ended up with

function VectorOfVector(::Type{T}, Nrow::Integer, Ncol::Integer) where {T}
    return [Vector{T}(undef, Nrow) for _ in Base.OneTo(Ncol)]

which is basically the same as your version. Thank you!