Sorry to dig up an old thread, but since this was referenced in another post I figured it was worth correcting the record.
The entire difference you’re observing between the vector-of-matrix approach and the 3D array approach here is due to your vector having a non-concrete element type. Your vec_mat
is of type Array{Matrix}
, but Matrix
is not a concrete type. You need Array{Matrix{Float64}}
. Likewise for your vector-of-vectors-of-vectors approach, you need an Array{Vector{Vector{Float64}}}
not Array{Vector{Vector}}
.
Switching to concretely typed structures makes the 3D array and vector-of-matrix approaches perform almost exactly the same:
julia> function f(x::Array{T, 3}) where T
x .+= 1
end
f (generic function with 2 methods)
julia> function f(x::Array{Matrix{T}}) where T
for matrix in x
matrix .+= 1
end
end
f (generic function with 2 methods)
julia> x_array = zeros(N, N, N);
julia> x_vec_mat = [zeros(N, N) for i in 1:N];
julia> using BenchmarkTools
julia> @btime f($x_array);
609.838 μs (0 allocations: 0 bytes)
julia> @btime f($x_vec_mat);
505.012 μs (0 allocations: 0 bytes)