Assigning values in for loop allocates memory

Why would that loop allocate anything at all?

julia> (M, N) = (512, 2048)
       A=zeros(M, N)
       v=zeros(M)
       @btime for k in 1:N
         v .= randn()
         A[:, k] = v
       end
  2.502 ms (10756 allocations: 200.08 KiB)

The loop below allocates so much more, why?

julia> @btime for k in 1:N
         v .= randn()
         for m in 1:M
            A[m, k] = v[m]
         end
       end
  112.815 ms (2110467 allocations: 48.27 MiB)

Maybe because you are measuring in global scope. Rewritten MWE

using BenchmarkTools

function test1()
    (M, N) = (512, 2048)
    A=zeros(M, N)
    v=zeros(M)
    for k in 1:N
        v .= randn()
        A[:, k] = v
    end
end

function test2()
    (M, N) = (512, 2048)
    A=zeros(M, N)
    v=zeros(M)
    for k in 1:N
        v .= randn()
        for m in 1:M
            A[m, k] = v[m]
        end
    end
end

@btime test1()
@btime test2()

shows

  2.234 ms (3 allocations: 8.00 MiB)
  2.337 ms (3 allocations: 8.00 MiB)

for me on Julia 1.6.3.

1 Like

I suggest reading through Performance Tips · The Julia Language before doing any performance measuring. It’s quite useful. The problem here is actually the first point on of the tips.

1 Like

Always a good advice. I actually did, but too long time ago.