# Help in understanding efficiently handling multidimensional arrays

**URL:** <https://discourse.julialang.org/t/help-in-understanding-efficiently-handling-multidimensional-arrays/19512>\
**Category:** New to Julia\
**Created:** [January 11, 2019, 12:50pm UTC](https://discourse.julialang.org/t/help-in-understanding-efficiently-handling-multidimensional-arrays/19512 "2019-01-11T12:50:03Z")\
**Posts on this page:** 1\
**Showing post:** 11

<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:14pm UTC](https://discourse.julialang.org/t/help-in-understanding-efficiently-handling-multidimensional-arrays/19512/11 "2019-01-12T18:14:57Z")

</div>

Here’s an example that tries to avoid the two issues I mentioned with the other benchmarks. In this case, i construct a 3D array of size 100x100x100 and also a vector of 100 100x100 matrices. I then benchmark adding one to each element, using the in-place version in both cases:

```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)

```

Both implementations are quite fast, and their performance on this task is comparable. In fact, the _entire_ difference between the vector-of-matrices and the 3D array approach shown in the [previous thread](https://discourse.julialang.org/t/vector-of-matrices-vs-multidimensional-arrays/9602/3) can be attributed to the two issues I referenced above (not updating the matrices in-place and working with non-concrete element types).

---

_[View the full topic](https://discourse.julialang.org/t/help-in-understanding-efficiently-handling-multidimensional-arrays/19512)._
