For example, this simple function:
function f(vec1,vec2)
for i in 2:length(vec1)
vec1[i] = vec1[i] - vec2[i-1]
end
nothing
end
I get:
julia> @btime f($vec1,$vec2)
759.284 ns (0 allocations: 0 bytes)
If I add @inbounds
to the inner loop, I get:
julia> @btime f($vec1,$vec2)
107.009 ns (0 allocations: 0 bytes)
Code
function f(vec1,vec2)
@inbounds for i in 2:length(vec1)
vec1[i] = vec1[i] - vec2[i-1]
end
nothing
end
But if I add @inbounds
to the function declaration as a whole, I get again the slow performance:
julia> @btime f($vec1,$vec2)
758.388 ns (0 allocations: 0 bytes)
Code
@inbounds function f(vec1,vec2)
for i in 2:length(vec1)
vec1[i] = vec1[i] - vec2[i-1]
end
nothing
end
It is not clear to me what is the actual block
inbounds acts upon. I tried Base.@propagage_inbounds
but the result is the same. I could not figure out from the manual how inbounds is, or not, propagating.
This also makes me wonder why the example of its use is something like:
for i = 1:length(A)
@inbounds r += A[i]
end
and not just adding it to the loop as a whole. I have seen this pattern often, but never figured out why one would do that instead of flagging the whole loop.