# 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:** 32

<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, 6:13am UTC](https://discourse.julialang.org/t/how-much-can-hard-coded-indexing-be-avoided-in-julia/55699/32 "2021-03-03T06:13:11Z")

</div>

Now nothing beats option E with `@inbounds`

```julia
@benchmark optA(A)
# BenchmarkTools.Trial: 
# memory estimate: 0 bytes
# allocs estimate: 0
# --------------
# minimum time: 18.172 ms (0.00% GC)
# median time: 20.093 ms (0.00% GC)
# mean time: 20.238 ms (0.00% GC)
# maximum time: 24.194 ms (0.00% GC)
# --------------
# samples: 248
# evals/sample: 1

@benchmark optAib(A)
# BenchmarkTools.Trial: 
# memory estimate: 0 bytes
# allocs estimate: 0
# --------------
# minimum time: 20.944 ms (0.00% GC)
# median time: 21.362 ms (0.00% GC)
# mean time: 21.542 ms (0.00% GC)
# maximum time: 24.136 ms (0.00% GC)
# --------------
# samples: 233
# evals/sample: 1

@benchmark optB(A)
# BenchmarkTools.Trial: 
# memory estimate: 0 bytes
# allocs estimate: 0
# --------------
# minimum time: 4.528 ms (0.00% GC)
# median time: 5.371 ms (0.00% GC)
# mean time: 5.395 ms (0.00% GC)
# maximum time: 8.741 ms (0.00% GC)
# --------------
# samples: 927
# evals/sample: 1

@benchmark optBib(A)
# BenchmarkTools.Trial: 
# memory estimate: 0 bytes
# allocs estimate: 0
# --------------
# minimum time: 3.606 ms (0.00% GC)
# median time: 3.724 ms (0.00% GC)
# mean time: 3.789 ms (0.00% GC)
# maximum time: 5.126 ms (0.00% GC)
# --------------
# samples: 1320
# evals/sample: 1

@benchmark optC(A)
# BenchmarkTools.Trial: 
# memory estimate: 0 bytes
# allocs estimate: 0
# --------------
# minimum time: 12.782 ms (0.00% GC)
# median time: 13.540 ms (0.00% GC)
# mean time: 13.619 ms (0.00% GC)
# maximum time: 16.399 ms (0.00% GC)
# --------------
# samples: 368
# evals/sample: 1

@benchmark optCib(A)
# BenchmarkTools.Trial: 
# memory estimate: 0 bytes
# allocs estimate: 0
# --------------
# minimum time: 2.949 ms (0.00% GC)
# median time: 3.240 ms (0.00% GC)
# mean time: 3.322 ms (0.00% GC)
# maximum time: 6.005 ms (0.00% GC)
# --------------
# samples: 1505
# evals/sample: 1

@benchmark optD(A)
# BenchmarkTools.Trial: 
# memory estimate: 0 bytes
# allocs estimate: 0
# --------------
# minimum time: 11.209 ms (0.00% GC)
# median time: 11.960 ms (0.00% GC)
# mean time: 12.088 ms (0.00% GC)
# maximum time: 18.776 ms (0.00% GC)
# --------------
# samples: 414
# evals/sample: 1

@benchmark optDib(A)
# BenchmarkTools.Trial: 
# memory estimate: 0 bytes
# allocs estimate: 0
# --------------
# minimum time: 2.452 ms (0.00% GC)
# median time: 2.898 ms (0.00% GC)
# mean time: 2.919 ms (0.00% GC)
# maximum time: 4.484 ms (0.00% GC)
# --------------
# samples: 1713
# evals/sample: 1

@benchmark optE(A)
# BenchmarkTools.Trial: 
# memory estimate: 0 bytes
# allocs estimate: 0
# --------------
# minimum time: 4.383 ms (0.00% GC)
# median time: 4.899 ms (0.00% GC)
# mean time: 4.963 ms (0.00% GC)
# maximum time: 7.618 ms (0.00% GC)
# --------------
# samples: 1008
# evals/sample: 1

@benchmark optEib(A)
# BenchmarkTools.Trial: 
# memory estimate: 0 bytes
# allocs estimate: 0
# --------------
# minimum time: 2.374 ms (0.00% GC)
# median time: 2.434 ms (0.00% GC)
# mean time: 2.515 ms (0.00% GC)
# maximum time: 4.093 ms (0.00% GC)
# --------------
# samples: 1987
# evals/sample: 1

```

At the risk of a digression, is there a way to activate/deactivate bounds checking as a startup option for Julia? I’m thinking that I would want bounds checking when actively coding, but deactivating it for running the .jl file, without having to manually use `@inbounds` in all the loops. Or, use `@inbounds` in all the loops, but having an option at startup to ignore that.

---

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