It’s not obvious to me why the following
func_array_eachrow4! function should allocate at all. Why should applying a function coming from an array be different than, say, applying the intrinsic
sin function inside the double loop?
function func_array_eachrow3!(W, f)
for i in 1:size(W, 1)
wi = view(W,i,:)
map!(f[i], wi, wi)
function func_array_eachrow4!(W, f)
for i = 1:size(W,1)
fn = f[i]
for j = 1:size(W,2)
W[i,j] = fn(W[i,j])
f_array = [sin, cos, sqrt]
W = rand(3, 10)
@btime func_array_eachrow3!(V, $f_array) setup=(V=copy(W))
@btime func_array_eachrow4!(V, $f_array) setup=(V=copy(W))
247.872 ns (6 allocations: 288 bytes)
2.344 μs (60 allocations: 960 bytes)
The same allocations happen if I write, e.g.
function func_array_eachrow5!(W, f)
but in this case we can imagine that the calculations are done in a separate temp array without mutating the original array
W, so allocations are justified here. Can someone comment on the above double loop please?