# How much can hard-coded indexing be avoided in Julia?

**URL:** <https://discourse.julialang.org/t/how-much-can-hard-coded-indexing-be-avoided-in-julia/55699>\
**Category:** Internals & Design\
**Created:** [February 20, 2021, 8:23pm UTC](https://discourse.julialang.org/t/how-much-can-hard-coded-indexing-be-avoided-in-julia/55699 "2021-02-20T20:23:42Z")\
**Posts on this page:** 1\
**Showing post:** 30

<div class="post-metadata">

**Author:** ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)\
**Post date:** [March 3, 2021, 5:50am UTC](https://discourse.julialang.org/t/how-much-can-hard-coded-indexing-be-avoided-in-julia/55699/30 "2021-03-03T05:50:02Z")

</div>

Thanks for pointing that out. Let’s try it again:

```julia
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

```

```julia
@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

```

```julia
@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

```

```julia
@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

```

```julia
@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

```

```julia
@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.

---

_[View the full topic](https://discourse.julialang.org/t/how-much-can-hard-coded-indexing-be-avoided-in-julia/55699)._
