# How to speedup setindex! and getindex?

**URL:** https://discourse.julialang.org/t/how-to-speedup-setindex-and-getindex/124413
**Category:** General Usage
**Tags:** speed-optimization
**Created:** [January 3, 2025, 4:29pm UTC](https://discourse.julialang.org/t/how-to-speedup-setindex-and-getindex/124413 "2025-01-03T16:29:23Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Thomas\_Van\_Giel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thomas_van_giel/32/46737_2.png) [@Thomas\_Van\_Giel](https://discourse.julialang.org/u/Thomas_Van_Giel)
#### Post date: [January 3, 2025, 4:29pm UTC](https://discourse.julialang.org/t/how-to-speedup-setindex-and-getindex/124413/1 "2025-01-03T16:29:23Z")

</div>

I have a piece of code where the setindex! and getindex function take a lot of time to execute. Quite simplified, the code looks a bit like this:

```julia
using Random

function main()
  A = rand(0:0.1:100000, 200,200)

  for step in 1:100000
    for i in 1:200
        for j in 1:200
          incr = (1 - 0.5) * 0.1
            A[i,j] += incr
        end
    end
  end
end

@profview main()

```

When I run the above code and profile the time it takes to execute, I get the following result:

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

Most of the time is spent using the setindex! function. Is there a way to speed this up, or do I have to live with the fact that it will be this slow.

---

<div class="post-metadata">

### Author: ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)
#### Post date: [January 3, 2025, 4:41pm UTC](https://discourse.julialang.org/t/how-to-speedup-setindex-and-getindex/124413/2 "2025-01-03T16:41:52Z")

</div>

I will try two answers

- this whole loop looks equivalent to `A .+= steps * incr`, but I assume this block is just a placeholder for more complex logic?
- of course all the work is in `setindex!` — the loop is doing nothing else! so I would answer your question with a question “what else would you expect to see in the profile”

---

<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 3, 2025, 4:41pm UTC](https://discourse.julialang.org/t/how-to-speedup-setindex-and-getindex/124413/3 "2025-01-03T16:41:59Z")

</div>

> [@Thomas\_Van\_Giel](#):
>
> ```julia
> for i in 1:200
> for j in 1:200
> incr = (1 - 0.5) * 0.1
> A[i,j] += incr
> 
> ```

Swap the order of your for loops; you’re not accessing sequential elements but rather skipping from column to column.

A decent rule of thumb is that you want the `i`s and `j`s to appear symmetrically in the order that’s written “closest” to the array, in both the indexing _and_ the for loops. In other words, you currently have the sequence `i` → `j` → `A` → `i` → `j`. This should be `j` → `i` → `A` → `i` → `j`

You can do even better by putting the `step` loop as the innermost one, but I’m guessing this is reflective of your real problem.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [January 3, 2025, 8:50pm UTC](https://discourse.julialang.org/t/how-to-speedup-setindex-and-getindex/124413/4 "2025-01-03T20:50:58Z")

</div>

> [@Thomas\_Van\_Giel](#):
>
> ```julia
> for i in 1:200
> for j in 1:200
> 
> ```

It’s not obvious which parts of this MWE reflects your real problem, but both for safety and for efficiency, you should use `for i in axes(A, 1)` etc, or simply `eachindex`. Then you can avoid bounds checks, which can significantly improve performance.
