Consider the following code
A = rand(1000,20); res = zeros(size(A,1)); @time for i in 1:size(A,1) p = view(A, i, :) res[i] = sum(p) end 0.004405 seconds (21.94 k allocations: 436.531 KiB)
Note the number of allocations and memory usage From what I understand the view costs some memory to be allocated and is being allocated in every iteration. But if view is an immutable struct then why is it not being optimized / reused in every iteration?
PS: I am aware of taking sum along a dimension, using sum here only as an example.