Preallocating array of matrices

To preallocate an array of matrices we can write the following:

A = Array{Matrix{Float64}}(undef, n)  # n being the number of array elements (matrices)

If we knew that each of the matrices in A has dimensions of 3 x 3, would declaring that in the preallocation bring any performance advantages? If so, how could we write that?

It’s the same like in
https://discourse.julialang.org/t/how-to-create-an-array-of-preallocated-mutable-structs
/18482

So:

julia> n=10
10

julia> a = [ Array{Float64,2}(undef,3,3) for i in 1:n ]
10-element Array{Array{Float64,2},1}:
 [8.00472116e-316 1.613449943e-315 1.04815513e-315; 1.866005866e-315 1.85975376e-315 8.0408006e-316; 1.866006024e-315 1.61345105e-315 8.00472116e-316]
 [8.0055939e-316 1.613454053e-315 1.61345453e-315; 8.0055939e-316 1.61345421e-315 1.613454686e-315; 1.166857286e-315 1.61345437e-315 1.748721164e-315]
 [1.166857286e-315 8.00472116e-316 1.748721875e-315; 8.00472116e-316 1.61345453e-315 0.0; 1.61345421e-315 1.613454686e-315 0.0]
 [1.866006024e-315 1.61345105e-315 8.00472116e-316; 1.613449943e-315 1.04815513e-315 0.0; 1.85975376e-315 8.0408006e-316 0.0]
 [1.5e-323 5.0e-324 0.0; 5.0e-324 5.0e-324 0.0; 5.0e-324 5.0e-324 0.0]
 [8.0055939e-316 6.9226573e-316 6.9226668e-316; 8.0055939e-316 6.9226605e-316 6.92266997e-316; 6.92265416e-316 6.92266364e-316 6.92267313e-316]
 [1.166857286e-315 0.0 0.0; 1.613454686e-315 0.0 0.0; 1.74872662e-315 0.0 0.0]
 [1.866006024e-315 0.0 0.0; 8.0408006e-316 0.0 0.0; 8.00472116e-316 0.0 0.0]
 [5.0e-324 0.0 0.0; 5.0e-324 0.0 0.0; 0.0 0.0 0.0]
 [6.92265416e-316 1.748716974e-315 1.748717053e-315; 8.0055939e-316 8.0055939e-316 6.9226573e-316; 8.0055939e-316 8.0055939e-316 6.9226605e-316]

Note: I have written Matrix{Float64} as Array{Float64,2}

Depending on what you are doing it could bring some performance advantages.

If you know for sure that every matrix is 3x3, then you should probably be using StaticArrays. Depending on your application, the performance benefits can be huge.

Higher dimensional arrays are an option here:

Array{Float64, 3}(undef,3,3,100)

Very interesting, I’ll check it out

Also a possibility, but in my case an array of matrices is much more convenient