Try and modify the result, and you’ll see the difference:
julia> A = [1 2; 3 4];
julia> B = A[:,:]; # This is actually a copy of A
julia> B[1,1]=0;
julia> A
2×2 Array{Int64,2}:
1 2
3 4
julia> B
2×2 Array{Int64,2}:
0 2
3 4
But:
julia> B = @view A[:,:]; # B is a view of A
julia> B[1,1]=0;
julia> A # Ooops!
2×2 Array{Int64,2}:
0 2
3 4
julia> B
2×2 view(::Array{Int64,2}, :, :) with eltype Int64:
0 2
3 4
In this particular case, it’s the same as B = A
, so @view
does not seem very useful. It makes more sense when you look at some slice, e.g. B = @view A[1,:]
(a view of the first row).
That’s useful if you want to make some calculations based on the values of particular rows, columns or blocks of a matrix, without having to make a copy of that portion of the array (which takes more memory and time). Just take care with the changes, as in the example!
@view
only applies to the slice next to the macro name; @views
makes views to all the slices right to it.