Consider the following code
using Distributions
using BenchmarkTools
u = randn(100, 2)
res = ones(100)
idx = 1
u_vector = u[:, idx]
@btime $res = $1.0 .- $u_vector;
@btime $res = $1.0 .- $u[:,idx];
@btime @views $res = $1.0 .- $u[:,idx];
These are the results that I got from the three lines with @btime
julia> @btime $res = $1.0 .- $u_vector;
37.478 ns (1 allocation: 896 bytes)
julia> @btime $res = $1.0 .- $u[:,idx];
607.383 ns (13 allocations: 1.97 KiB)
julia> @btime @views $res = $1.0 .- $u[:,idx];
397.597 ns (6 allocations: 1.08 KiB)
The second @btime line has the greatest amount of time and allocations but that's in line with my expectation, since I'm slicing. However, I'm not sure why the third line with @views is not the same as the first line? I thought by using @views I'm not longer creating a copy. Is there a way to "fix" the third line? In my real code, the user provides idx so idx is not known in advance. Therefore, I would want to reduce allocations when I do slicing.
What I assume you are looking for is:
julia> @btime $res .= 1.0 .- view($u, :,$idx);
13.126 ns (0 allocations: 0 bytes)
The point is that you want to avoid allocation of the vector on RHS, and that is why you should use .= not =.
I also changed @views to view call. It does not matter here, but, in general using @views is tricky at times, and I avoid it unless there is a reason, see here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With