# Substituting elements of array is very slow

**URL:** https://discourse.julialang.org/t/substituting-elements-of-array-is-very-slow/65036
**Category:** General Usage
**Created:** [July 21, 2021, 2:45pm UTC](https://discourse.julialang.org/t/substituting-elements-of-array-is-very-slow/65036 "2021-07-21T14:45:58Z")
**Posts on this page:** 4
**Page:** 2

<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: [July 21, 2021, 10:22pm UTC](https://discourse.julialang.org/t/substituting-elements-of-array-is-very-slow/65036/21 "2021-07-21T22:22:40Z")

</div>

> [@mscheffel](#):
>
> `Memory estimate: 2.35 MiB, allocs estimate`

Did you remember the dollar sign? As in

> [@DNF](#):
>
> ```julia
> @benchmark testfun02($B)
> @btime testfun02($B)
> 
> ```

---

<div class="post-metadata">

### Author: ![mscheffel](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@mscheffel](https://discourse.julialang.org/u/mscheffel)
#### Post date: [July 21, 2021, 10:30pm UTC](https://discourse.julialang.org/t/substituting-elements-of-array-is-very-slow/65036/22 "2021-07-21T22:30:21Z")

</div>

Yes, the dollar sign was included.

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [July 21, 2021, 10:41pm UTC](https://discourse.julialang.org/t/substituting-elements-of-array-is-very-slow/65036/23 "2021-07-21T22:41:08Z")

</div>

If I run exactly the code you posted [here](https://discourse.julialang.org/t/substituting-elements-of-array-is-very-slow/65036/11), and taking care of using the `$` in the benchmarking, I get:

```julia
julia> benchout = @benchmark testfun02($B);

julia> benchout
BechmarkTools.Trial: 54 samples with 1 evaluations.
 Range (min … max): 93.013 ms … 102.730 ms ┊ GC (min … max): 0.00% … 0.00%
 Time (median): 93.293 ms ┊ GC (median): 0.00%
 Time (mean ± σ): 93.632 ms ± 1.378 ms ┊ GC (mean ± σ): 0.00% ± 0.00%

    █                                                           
  ▄▃█▇▄▄▃▄▅▄▅▄▄▁▁▁▁▁▁▁▁▁▁▁▃▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▃ ▁
  93 ms Histogram: frequency by time 96.9 ms <

 Memory estimate: 234.45 KiB, allocs estimate: 2.

```

I don’t think you should be surprised by the cost of the memory access. The operations are very fast in the processor, and here you almost don’t do anything.

If you run over the columns and rows in the incorrect order, you get a code that is 10x slower, and that is only because of improper memory access order, that gives you an idea.

This is why it is so important to use immutable structures and avoid allocations and memory accesses in critical code.

---

<div class="post-metadata">

### Author: ![mscheffel](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@mscheffel](https://discourse.julialang.org/u/mscheffel)
#### Post date: [July 22, 2021, 7:35am UTC](https://discourse.julialang.org/t/substituting-elements-of-array-is-very-slow/65036/24 "2021-07-22T07:35:40Z")

</div>

Thanks. That is probably the explanation. I have to be very careful for my project development as speed plays a crucial role. Thanks all for the support

[Previous page](https://discourse.julialang.org/t/substituting-elements-of-array-is-very-slow/65036.md?page=1)
