I found an issue while using @views, which may help others speed up their code. Let us consider this example of code.
len = 5000
const arr1 = Array{Float64, 2}(undef, 10, len)
const arr2 = Array{Float64, 2}(undef, 10, len)
@time for i in 1:len
@views arr1[:] .*= arr2[:]
end
The output is “1.454046 seconds (53.98 k allocations: 1.864 GiB, 12.21% gc time)”. As we can see here it allocates 1.864GiB memory, and GC worked here. I was wondering if this happens due to the design of @views or it is simply a bug (in which case, I can open an issue).
I also found two ways how can we avoid this problem. We can write “@views arr1[:,:] .*= arr2[:,:]” instead of line 5 and the output will be “0.235785 seconds (13.98 k allocations: 296.562 KiB)”.
Or we can write 5th line this way “(@view arr1[:]) .*= @view arr2[:]” and the output will be “0.363380 seconds (33.98 k allocations: 1.053 MiB)”.
So as you can see in both cases there is no such huge allocation and the running time is much less than in the original example. There is no such issue with 1-dimensional arrays.