АО
Если взять то, что он пишет и выделить часть, которая занимается изменением данных, то выглядит так:
using Statistics
using Setfield
struct Agent2I
loc::Int
junk::NTuple{100, Int}
end
mutable struct Agent2M
loc::Int
junk::NTuple{100, Int}
end
const REF_TUP = ntuple(i -> 0, 100)
const N = 10^5
xI = [Agent2I(i, REF_TUP) for i in 1:N]
xM = [Agent2M(i, REF_TUP) for i in 1:N]
function gI(x)
for i in eachindex(x)
xi = x[i]
x[i] = @set xi.loc += 1
end
return mean(x -> x.loc, x)
end
function gM(x)
for i in eachindex(x)
x[i].loc += 1
end
return mean(x -> x.loc, x)
end
и тесты соответственно
julia> using BenchmarkTools
julia> @benchmark gI(x) setup=(x = deepcopy(xI)) evals=1
BenchmarkTools.Trial:
memory estimate: 16 bytes
allocs estimate: 1
--------------
minimum time: 2.074 ms (0.00% GC)
median time: 2.577 ms (0.00% GC)
mean time: 2.729 ms (0.00% GC)
maximum time: 4.656 ms (0.00% GC)
--------------
samples: 108
evals/sample: 1
julia> @benchmark gM(x) setup=(x = deepcopy(xM)) evals=1
BenchmarkTools.Trial:
memory estimate: 16 bytes
allocs estimate: 1
--------------
minimum time: 2.294 ms (0.00% GC)
median time: 2.447 ms (0.00% GC)
mean time: 2.691 ms (0.00% GC)
maximum time: 3.729 ms (0.00% GC)
--------------
samples: 9
evals/sample: 1
