Want to verify that I’ve done this in a way that gives valid results.
In the code below dostuff! updates 2 arrays in place. clos! creates a closure that encloses the arrays and accepts arguments for the change factors.
Function doitem! is closer to what I do in my code: one element of one or more arrays is modified in place. clositem! encloses the arrays and accepts arguments for the changes.
The good news, if I’ve done this correctly, there appears to be no performance penalty for enclosing the arrays. That’s great because the code where I modify arrays is in a very hot loop.
The slight surprise is that there is a higher % penalty (though quite small in absolute time) when modifying a single element of each array.
Comments, corrections, suggestions welcome!
Code:
function buildit()
print("\nEnter n: "); n = parse(Int64, (chomp(readline())))
return rand(n), collect(1:n)
end
function dostuff!(xfl, xint, arr1, arr2)
arr1 .*= xfl
arr2 .+= xint
end
function doitem!(xfl, xint, item, arr1, arr2)
arr1[item] *= xfl
arr2[item] += xint
end
arrfl, arrint = buildit()
clos!(xfl, xint) = dostuff!(xfl, xint, arrfl, arrint)
clositem!(xfl, xint, item) = doitem!(xfl, xint, item, arrfl, arrint)
Benchmarks for arrays with 100_000 elements:
julia> @btime dostuff!(1.0, 1, arrfl, arrint);
2.903 μs (0 allocations: 0 bytes)
julia> @btime clos!(1.0, 1);
2.907 μs (1 allocation: 16 bytes)
julia> @btime doitem!(1.0, 1, 25, arrfl, arrint)
13.847 ns (1 allocation: 16 bytes)
julia> @btime clositem!(1.0, 1, 25)
19.121 ns (2 allocations: 32 bytes)