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> 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 can be attributed to the two issues I referenced above (not updating the matrices in-place and working with non-concrete element types).