What is the recommended/best Julia method to create a vector of vectors of variable lengths?
Current method MWE:
# test_vector_of_vectors.jl
begin
vec_of_vecs = [Float64[] for _ in 1:3]
for i = 1:3
push!(vec_of_vecs[1], 0.0)
end
for i = 1:5
push!(vec_of_vecs[2], 1.0)
end
for i = 1:7
push!(vec_of_vecs[3], 2.0)
end
@show vec_of_vecs
end
[In the actual code, the variable length vector are workspace arrays, , all initialized to 0.0, to all be passed to a set of functions.]
If the individual sizes can be calculated from the index. If not I would do it more like:
sizes=[ 7, 3, 5 ]
vec_of_vecs = [zeros(Float64,sizes[s]) for s in 1:3]
Don’t know if this is recommended (by whom) or the best (in what sense). If it is important to have the best performance, because it’s perhaps in a hot loop, my guess is, complete performance depends more on the whole thing you want to do, the context.