# OffsetArrays, inbounds, and confusion

**URL:** https://discourse.julialang.org/t/offsetarrays-inbounds-and-confusion/81295
**Category:** Internals & Design
**Tags:** indexing, arrays
**Created:** [May 19, 2022, 3:50am UTC](https://discourse.julialang.org/t/offsetarrays-inbounds-and-confusion/81295 "2022-05-19T03:50:46Z")
**Posts on this page:** 1
**Showing post:** 31

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [May 19, 2022, 4:24pm UTC](https://discourse.julialang.org/t/offsetarrays-inbounds-and-confusion/81295/31 "2022-05-19T16:24:59Z")

</div>

Not too tough:

```julia
julia> function f!(A)
           for (count, idx) in enumerate(eachindex(A))
               A[idx] = count*2
           end
       end
f! (generic function with 1 method)

julia> f!(A); @allocated f!(A)
0

```

Or this works as a slightly less-quick sanity check, too:

```julia
julia> using BenchmarkTools

julia> function g!(A)
           i = 0
           for idx in eachindex(A)
               i += 1
               A[idx] = i*2
           end
       end

julia> @btime f!($A)
  11.029 ns (0 allocations: 0 bytes)

julia> @btime g!($A)
  13.349 ns (0 allocations: 0 bytes)

```

Amusingly, that difference does seem real — your “comfortable” construction is actually slower 😛

---

_[View the full topic](https://discourse.julialang.org/t/offsetarrays-inbounds-and-confusion/81295)._
