# Vector of matrices vs. multidimensional arrays

**URL:** <https://discourse.julialang.org/t/vector-of-matrices-vs-multidimensional-arrays/9602>\
**Category:** Performance\
**Created:** [March 8, 2018, 5:50pm UTC](https://discourse.julialang.org/t/vector-of-matrices-vs-multidimensional-arrays/9602 "2018-03-08T17:50:05Z")\
**Posts on this page:** 1\
**Showing post:** 9

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [January 12, 2019, 6:17pm UTC](https://discourse.julialang.org/t/vector-of-matrices-vs-multidimensional-arrays/9602/9 "2019-01-12T18:17:54Z")

</div>

Sorry to dig up an old thread, but since this was referenced in [another post](https://discourse.julialang.org/t/help-in-understanding-efficiently-handling-multidimensional-arrays/19512/11) 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
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)

```

---

_[View the full topic](https://discourse.julialang.org/t/vector-of-matrices-vs-multidimensional-arrays/9602)._
