I was wondering why making a[range] = b[range]
seems to make unnecessary allocations and if (why) that is intended. For example:
function f!(a, b, n)
a[1:n, 1:n] = b[1:n, 1:n]
end
function g!(a, b, n)
copyto!(a, CartesianIndices((1:n, 1:n)), b, CartesianIndices((1:n, 1:n)))
end
function h!(a, b, n)
for i = 1:n
for k = 1:n
a[k, i] = b[k, i]
end
end
end
n = 500
a = zeros(n,n)
b = rand(n,n)
@time f!(a,b,n); # 0.002301 seconds (6 allocations: 1.908 MiB)
@time g!(a,b,n); # 0.000327 seconds (4 allocations: 160 bytes)
@time h!(a,b,n); # 0.000492 seconds (4 allocations: 160 bytes)
Doing the for loops (h!
) seems to be the optimal way allocation-wise if the idea is to make a copy from transpose(b)
(inside the function) or any kind of column or line permutation.