Filling pre-allocated arrays with minimal function allocations

You should put $ signs before your variables when you want to pass them to @btime like this:

julia> @btime func!($x1, $x2)
  3.472 ns (0 allocations: 0 bytes)

In general you should make your global variabls const as much as possible so the compilers can make better optimizations:

const x3 = zeros(3)
const x4 = ones(3)

julia> @btime func!(x3, x4) # notice variables without $ sign
  3.460 ns (0 allocations: 0 bytes) 
1 Like