# Iteration with CartesianIndices(array) is slow when the dimension of the array is large

**URL:** <https://discourse.julialang.org/t/iteration-with-cartesianindices-array-is-slow-when-the-dimension-of-the-array-is-large/27951>\
**Category:** Performance\
**Tags:** indexing\
**Created:** [August 25, 2019, 12:38am UTC](https://discourse.julialang.org/t/iteration-with-cartesianindices-array-is-slow-when-the-dimension-of-the-array-is-large/27951 "2019-08-25T00:38:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomohiro\_soejima](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomohiro_soejima/32/8056_2.png) [@tomohiro\_soejima](https://discourse.julialang.org/u/tomohiro_soejima)\
**Post date:** [August 25, 2019, 12:38am UTC](https://discourse.julialang.org/t/iteration-with-cartesianindices-array-is-slow-when-the-dimension-of-the-array-is-large/27951/1 "2019-08-25T00:38:36Z")

</div>

I’ve been playing around with `CartesianIndices`. It works beautifully for the most part, but I’ve noticed that it becomes slower compared to linear indexing, when the dimension of the array is large. Here is a minimal example using Julia 1.2.0.

```julia
using BenchmarkTools

#input is 5-dimensional array
dim_5 = rand(Float64, (27*ones(Int64,5))...)
#input is 15-dimensional array
dim_15 = rand(Float64,(3*ones(Int64, 15))...)
#the number of elements is 3^15 = 14,348,907 for both of them

function cartesiansum(array)
    sum = zero(eltype(array))
    for index in CartesianIndices(array)
        sum += array[index]
    end
    return sum
end

function linearsum(array)
    sum = zero(eltype(array))
    for index in eachindex(array)
        sum += array[index]
    end
    return sum
end

println("Cartesian sum with dimension = 5")
@btime cartesiansum(dim_5)
println("Linear sum with dimension = 5")
@btime linearsum(dim_5)
println("Cartesian sum with dimension = 15")
@btime cartesiansum(dim_15)
println("Linear sum with dimension = 15")
@btime linearsum(dim_15)

```

Note that I made the number of elements to be the same between dim\_5, dim\_15. Here is the output.

```julia
Cartesian sum with dimension = 5
  84.114 ms (1 allocation: 16 bytes)
Linear sum with dimension = 5
  24.007 ms (1 allocation: 16 bytes)
Cartesian sum with dimension = 15
  407.303 ms (1 allocation: 16 bytes)
Linear sum with dimension = 15
  25.045 ms (1 allocation: 16 bytes)

```

As you can see, there is a significant slowdown when I use CartesianIndices for dimension 15 array.

Is there any workaround around this. or are we advised not to use CartesianIndices for large dimension arrays?

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [August 25, 2019, 8:59am UTC](https://discourse.julialang.org/t/iteration-with-cartesianindices-array-is-slow-when-the-dimension-of-the-array-is-large/27951/2 "2019-08-25T08:59:38Z")

</div>

> [@tomohiro\_soejima](#):
>
> Is there any workaround around this. or are we advised not to use CartesianIndices for large dimension arrays?

Don’t use CartesianIndices full stop, unless absolutely unavoidable. Consider whether you _really_ need large dimensional arrays at all. Indexing into `Array` always converts into linear indices in order to compute addresses. This involves a little bit of offset computation (integer addition and multiplication).

Just look at `@code_native getindex(myarray, first(CartesianIndices(myarray)))` versus `getindex(myarray, first(eachindex(myarray)))` and weep (and the code looks ok, it’s just a consequence of what you ask your computer to do).

---

<div class="post-metadata">

**Author:** ![tomohiro\_soejima](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomohiro_soejima/32/8056_2.png) [@tomohiro\_soejima](https://discourse.julialang.org/u/tomohiro_soejima)\
**Post date:** [August 25, 2019, 9:06am UTC](https://discourse.julialang.org/t/iteration-with-cartesianindices-array-is-slow-when-the-dimension-of-the-array-is-large/27951/3 "2019-08-25T09:06:39Z")

</div>

That makes sense. I’ll take a look at those codes. Thanks!
