# 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:** 3

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [March 8, 2018, 6:07pm UTC](https://discourse.julialang.org/t/vector-of-matrices-vs-multidimensional-arrays/9602/3 "2018-03-08T18:07:41Z")

</div>

Before anything else, read the [Performance Tips](https://docs.julialang.org/en/latest/manual/performance-tips/)–you shouldn’t work with non-constant variables in global scope.

```julia
using BenchmarkTools

# declare global-scope variables as constants, otherwise the compiler can't
# stably infer the variable's type
const L = 500 # integer literals in Julia don't require conversion
const v3 = [[rand(L) for i = 1:L] for j = 1:L]
const vec_mat = Array{Matrix}(L);
const arr = rand(L, L, L);

fill!(vec_mat, rand(L, L));

@btime v3 .+= 1.0
@btime vec_mat .+= 1.0
@btime arr .+= 1.0

```

```julia
  5.767 s (250500 allocations: 993.80 MiB)
  5.411 s (1000 allocations: 953.71 MiB)
  86.659 ms (0 allocations: 0 bytes)

```

---

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