Thanks for pointing that out. Let’s try it again:
using BenchmarkTools
f(x, y) = x*y
g(x, y) = x*y
const A = Array{Float64}(undef, 100, 200, 300)
# option A
function optA(A)
for i in 1:100, j in 1:200, k in 1:300
A[i, j, k] = f(i, g(j, k))
end
end
# option B
function optB(A)
for k in 1:300, j in 1:200, i in 1:100
A[i, j, k] = f(i, g(j, k))
end
end
# option C
function optC(A)
for (idx, _) in pairs(A)
i, j, k = Tuple(idx)
A[idx] = f(i, g(j, k))
end
end
# option D
function optD(A)
for (idx, _) in pairs(A)
i, j, k = Tuple(idx)
A[i, j, k] = f(i, g(j, k))
end
end
# option E
function optE(A)
for idx in CartesianIndices(A)
i, j, k = Tuple(idx)
A[idx] = f(i, g(j, k))
end
end
@benchmark optA(A)
BenchmarkTools.Trial:
memory estimate: 0 bytes
allocs estimate: 0
--------------
minimum time: 19.437 ms (0.00% GC)
median time: 23.235 ms (0.00% GC)
mean time: 22.935 ms (0.00% GC)
maximum time: 28.840 ms (0.00% GC)
--------------
samples: 218
evals/sample: 1
@benchmark optB(A)
BenchmarkTools.Trial:
memory estimate: 0 bytes
allocs estimate: 0
--------------
minimum time: 4.404 ms (0.00% GC)
median time: 4.742 ms (0.00% GC)
mean time: 4.897 ms (0.00% GC)
maximum time: 8.086 ms (0.00% GC)
--------------
samples: 1021
evals/sample: 1
@benchmark optC(A)
BenchmarkTools.Trial:
memory estimate: 0 bytes
allocs estimate: 0
--------------
minimum time: 12.557 ms (0.00% GC)
median time: 13.302 ms (0.00% GC)
mean time: 13.348 ms (0.00% GC)
maximum time: 17.289 ms (0.00% GC)
--------------
samples: 375
evals/sample: 1
@benchmark optD(A)
BenchmarkTools.Trial:
memory estimate: 0 bytes
allocs estimate: 0
--------------
minimum time: 12.493 ms (0.00% GC)
median time: 13.546 ms (0.00% GC)
mean time: 13.587 ms (0.00% GC)
maximum time: 16.432 ms (0.00% GC)
--------------
samples: 368
evals/sample: 1
@benchmark optE(A)
BenchmarkTools.Trial:
memory estimate: 0 bytes
allocs estimate: 0
--------------
minimum time: 4.355 ms (0.00% GC)
median time: 4.733 ms (0.00% GC)
mean time: 4.851 ms (0.00% GC)
maximum time: 7.987 ms (0.00% GC)
--------------
samples: 1031
evals/sample: 1
So now options B and E are the most performant. Option E is the most amenable to human sanity.