# @inbounds does nothing when converting Cartesian to linear Index

**URL:** <https://discourse.julialang.org/t/inbounds-does-nothing-when-converting-cartesian-to-linear-index/109201>\
**Category:** Performance\
**Tags:** question\
**Created:** [January 24, 2024, 3:23pm UTC](https://discourse.julialang.org/t/inbounds-does-nothing-when-converting-cartesian-to-linear-index/109201 "2024-01-24T15:23:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)\
**Post date:** [January 24, 2024, 3:23pm UTC](https://discourse.julialang.org/t/inbounds-does-nothing-when-converting-cartesian-to-linear-index/109201/1 "2024-01-24T15:23:43Z")

</div>

Hi,

I do not understand the profiling of the following MWE.

```julia
using BenchmarkTools
using Random

function weirdsum(a,lis,ci)
    li = @inbounds lis[ci]
    ac = @inbounds a[ci]
    al = @inbounds a[li]
    return ac+al
end

function testli()
    Random.seed!(123)
    a=rand(4,5,6)
    cis = CartesianIndices(a)
    lis = LinearIndices(cis)
    s =0.0
    @inbounds for i = 1:1000_000_000
        ci = rand(cis)
        s+=weirdsum(a,lis,ci)
    end
    @show s
end

testli()

```

It seems that a significant time proportion is spent in bound checking (line 5 : `li = @inbounds lis[ci]`) despite the @inbounds macro.

 ![cropweird](https://global.discourse-cdn.com/julialang/original/3X/d/5/d57a3c88bc5e5559c6178709e026c983b6a02001.png)

Any hint ?

Laurent

---

<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:** [January 24, 2024, 3:35pm UTC](https://discourse.julialang.org/t/inbounds-does-nothing-when-converting-cartesian-to-linear-index/109201/2 "2024-01-24T15:35:55Z")

</div>

I don’t see that behavior. Can you reproduce it in a fresh Julia session?

---

<div class="post-metadata">

**Author:** ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)\
**Post date:** [January 24, 2024, 3:49pm UTC](https://discourse.julialang.org/t/inbounds-does-nothing-when-converting-cartesian-to-linear-index/109201/3 "2024-01-24T15:49:11Z")

</div>

I have just created a fresh project and use a restarted VS Code session (for @profview)  
and I get the same result.

Just in case, here is the result of versioninfo()

```julia
julia> include("toto.jl")
s = 9.716345882702819e8
9.716345882702819e8

julia> @profview testli()
s = 9.716345882702819e8

julia> versioninfo()
Julia Version 1.10.0
Commit 3120989f39b (2023-12-25 18:01 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: macOS (arm64-apple-darwin22.4.0)
  CPU: 10 × Apple M1 Max
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-15.0.7 (ORCJIT, apple-m1)
  Threads: 11 on 8 virtual cores
Environment:
  JULIA_EDITOR = code
  JULIA_NUM_THREADS = 12

julia> 

```

---

<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:** [January 24, 2024, 3:55pm UTC](https://discourse.julialang.org/t/inbounds-does-nothing-when-converting-cartesian-to-linear-index/109201/4 "2024-01-24T15:55:56Z")

</div>

Is there anything in your startup? Or does `--check-bounds=yes` appear in `Base.julia_cmd()`?

I only see `checkbounds` in my profiles if I explicitly start Julia with `--check-bounds=yes`.

> **My terminal session**
>
> ```bash
> ▶ julia +1.10 --startup-file=no -e 'using Random
> 
> function weirdsum(a,lis,ci)
> li = @inbounds lis[ci]
> ac = @inbounds a[ci]
> al = @inbounds a[li]
> return ac+al
> end
> 
> function testli()
> Random.seed!(123)
> a=rand(4,5,6)
> cis = CartesianIndices(a)
> lis = LinearIndices(cis)
> s =0.0
> @inbounds for i = 1:1000_000_000
> ci = rand(cis)
> s+=weirdsum(a,lis,ci)
> end
> @show s
> end
> 
> testli()
> using Profile
> @profile testli()
> Profile.print()' | grep 'check'
> 
> ▶ julia +1.10 --startup-file=no --check-bounds=yes -e 'using Random
> 
> function weirdsum(a,lis,ci)
> li = @inbounds lis[ci]
> ac = @inbounds a[ci]
> al = @inbounds a[li]
> return ac+al
> end
> 
> function testli()
> Random.seed!(123)
> a=rand(4,5,6)
> cis = CartesianIndices(a)
> lis = LinearIndices(cis)
> s =0.0
> @inbounds for i = 1:1000_000_000
> ci = rand(cis)
> s+=weirdsum(a,lis,ci)
> end
> @show s
> end
> 
> testli()
> using Profile
> @profile testli()
> Profile.print()' | grep 'check'
> 1╎ ╎ 1 ...abstractarray.jl:697; checkbounds
> ╎ ╎ 251 ...abstractarray.jl:699; checkbounds
> ╎ ╎ 251 ...abstractarray.jl:684; checkbounds
> 246╎ ╎ 246 ...abstractarray.jl:697; checkbounds
> 251╎ ╎ 279 ...abstractarray.jl:699; checkbounds
> ╎ ╎ ╎ 28 ...bstractarray.jl:678; checkbounds
> ╎ ╎ 386 .../abstractarray.jl:699; checkbounds
> ╎ ╎ 386 .../abstractarray.jl:678; checkbounds
> ╎ ╎ 386 ...abstractarray.jl:725; checkbounds_indices
> ╎ ╎ 385 ...abstractarray.jl:725; checkbounds_indices
> ╎ ╎ ╎ 385 ...bstractarray.jl:725; checkbounds_indices
> ╎ ╎ ╎ 385 ...bstractarray.jl:760; checkindex
> ╎ ╎ 1 ...abstractarray.jl:760; checkindex
> 52╎ ╎ 57 .../abstractarray.jl:699; checkbounds
> ╎ ╎ 5 ...abstractarray.jl:684; checkbounds
> ╎ ╎ 5 ...abstractarray.jl:760; checkindex
> 
> ```

---

<div class="post-metadata">

**Author:** ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)\
**Post date:** [January 24, 2024, 4:01pm UTC](https://discourse.julialang.org/t/inbounds-does-nothing-when-converting-cartesian-to-linear-index/109201/5 "2024-01-24T16:01:29Z")

</div>

> [@mbauman](#):
>
> `Base.julia_cmd()`

You are right ! Thank you very much for your help !

```julia
julia> Base.julia_cmd()
`/Users/laurentplagne/.julia/juliaup/julia-1.10.0+0.aarch64.apple.darwin14/bin/julia -C native -J/Users/laurentplagne/.julia/juliaup/julia-1.10.0+0.aarch64.apple.darwin14/lib/julia/sys.dylib --check-bounds=yes -g1`

```
