Full MWE:
MWE
using StaticArrays, Parameters, Accessors, BenchmarkTools
# Test Functions
function loop1(dat)
for t in 1:10
for i in eachindex(dat)
@reset dat[i].A .+= dat[i].B
end
end
return dat
end
function loop2(dat)
for t in 1:10
@reset dat[1].A .+= dat[1].B
@reset dat[2].A .+= dat[2].B
end
return dat
end
# Main
@with_kw struct Str
A :: SVector{10, Int16}
B :: SVector{10, Int16}
end
str = Str(A = SVector{10, Int16}(1:10), B = SVector{10, Int16}(11:20));
dat = (str, deepcopy(str));
res1 = loop1(dat);
res2 = loop2(dat);
# Expected Results
hcat(res1[1].A, res1[2].A)
res1 == res2
# Benchmarks
@btime loop1(x) setup = (x = $dat) evals = 1;
@btime loop2(x) setup = (x = $dat) evals = 1;
@btime loop1($dat);
@btime loop2($dat);
I have two structs of SVectors stored in a tuple, like so:
@with_kw struct Str
A :: SVector{10, Int16}
B :: SVector{10, Int16}
end
str = Str(A = SVector{10, Int16}(1:10), B = SVector{10, Int16}(11:20));
dat = (str, deepcopy(str));
I wanted to increment the values in those SVectors and tried two different methods:
function loop1(dat)
for t in 1:10
for i in eachindex(dat)
@reset dat[i].A .+= dat[i].B
end
end
return dat
end
function loop2(dat)
for t in 1:10
@reset dat[1].A .+= dat[1].B
@reset dat[2].A .+= dat[2].B
end
return dat
end
Both give the correct answer:
julia> hcat(res1[1].A, res1[2].A)
10Ć2 SMatrix{10, 2, Int16, 20} with indices SOneTo(10)ĆSOneTo(2):
111 111
122 122
133 133
144 144
155 155
166 166
177 177
188 188
199 199
210 210
julia> res1 == res2
true
But I donāt know what to make of these Benchmarks:
julia> @btime loop1(x) setup = (x = $dat) evals = 1;
709.000 ns (0 allocations: 0 bytes)
julia> @btime loop2(x) setup = (x = $dat) evals = 1;
28.000 ns (0 allocations: 0 bytes)
julia> @btime loop1($dat);
581.249 ns (0 allocations: 0 bytes)
julia> @btime loop2($dat);
5.040 ns (0 allocations: 0 bytes)
- Why is loop1 so much slower than loop2?
- Why is the number of evals so important when benchmarking this?
I would much prefer to use the loop1 approach, but it is 100x slower despite no allocations. Any ideas? Thanks.