Vectorised vs Devectorised Performance

x = a + b

creates a new array from the result of a + b, and assigns the label x to it. This new x has nothing to do with the x = [...] that was allocated previously, before the loop.

If one wants to explicitly mutate the previous x, than one could use the “devectorized” alternative, or:

x .= a .+ b

(note the dots). That is a syntax sugar for

for index in eachindex(x,a,b)
    @inbounds x[index] = a[index] + b[index]
end

(which is the same as the “devectorized” vesion, but will error if x, a, and b do not have the same length, such that @inbounds is good there - as it will be assumed in the dot sugar).

Probably this is a good read here: Assignment and mutation · JuliaNotes.jl

2 Likes