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)
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.