I'm trying to optimize the speed and allocation of this loop
function loop(n,k)
m = rand(10,n)
r = rand(10,1)
for j in 1:k
for i in 2:size(m,2)
@inbounds m[:,i-1] = m[:,i] + rand!(r)
end
end
end
The memory allocation is quite big: @time loop(10000,30) has an allocation of 599.94k, increasing in k. I think there are two contributing factors: (1) allocation of m[:,i] and (2) allocation of m[:,i-1]. I'm hoping that @inbounds can help, but it doesn't. Removing @inbounds doesn't help with the allocation.
Is there a way to reduce allocations? I'm really not creating any new objects, so it should be invariant to k. I tried to replace @inbounds with @view but it didn't even run. I don't think I can use broadcast! here.