I have to deal with 3 dimensional structures, I was hesitating between vectors of vectors of vectors, vectors of matrices or tridimensional arrays. For performance comparison, I wrote this small test:
julia> L = M = N = Int(5e2);
julia> vec_vec_vec = Array{Array{Vector}}(L);
julia> for i = 1:L vec_vec_vec[i] = [zeros(N) for j = 1:M] end;
julia> vec_mat = Array{Matrix}(L);
julia> fill!(vec_mat, rand(M, N));
julia> arr = rand(L, M, N);
julia> tic(); for i = 1:L for j = 1:M for k = 1:N vec_vec_vec[i][j][k] += 1; end; end; end; t = toc(); println(t)
elapsed time: 28.747643437 seconds
28.747643437
julia> tic(); for i = 1:L for j = 1:M for k = 1:N vec_mat[i][j,k] += 1; end; end; end; t = toc(); println(t)
elapsed time: 25.837854348 seconds
25.837854348
julia> tic(); for i = 1:L for j = 1:M for k = 1:N arr[i,j,k] += 1; end; end; end; t = toc(); println(t)
elapsed time: 100.412620007 seconds
100.412620007
Can someone explain to me why it is so much more expensive to use a 3d array rather than a vector of matrices, and why the vec_vec_vec and vec_mat solutions are similar? I understand that this example just looks at loops over the whole structures, and even in my case I need to perform matrix-related operations so vec_vec_vec is not very suitable. The ideal solution for me would have been to use the 3d array, but this wouldn’t be reasonable with such performances…
Many thanks