Broadcasting inconsistency between addition and multiplication

The fact that scalar multiplication doesn’t use broadcasting also means that it doesn’t get to benefit from loop fusion, which can be important for performance when you’re chaining operations.

julia> using BenchmarkTools

julia> let A = rand(100, 100), B = similar(A)
    @btime $B .= 2  * $A .+ 1
    @btime $B .= 2 .* $A .+ 1
end;
  6.234 μs (2 allocations: 78.20 KiB)
  1.915 μs (0 allocations: 0 bytes)

Dots are good! You want more of them, not less. :slight_smile:

5 Likes