# 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:** 1\
**Showing post:** 11

<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, 6:35pm UTC](https://discourse.julialang.org/t/substituting-elements-of-array-is-very-slow/65036/11 "2021-07-21T18:35:45Z")

</div>

Thanks. Here is the complete code I am running. It is actually just the LinearAlgebra and the BenchmarkTools missing

```julia
using LinearAlgebra
using BenchmarkTools

function testfun02(M)
    nrow, ncol = size(M);
    spidx = Array{Float64, 2}(undef, nrow, 3)
    numit = 1
    
    for j01 = 1:ncol, j02 = 1:nrow
        aval = M[j02,j01]*(j01+j02)
        if aval > 0
           spidx[numit,1] = j01
           spidx[numit,2] = j02
           spidx[numit,3] = aval
            numit += 1
        end
    end
    return spidx 
end

B = Array{Float64,2}(I, 10000, 10000);
benchout = @benchmark testfun02(B);
@time testfun02(B);
@time testfun02(B);

```

The Output is

```julia
@time testfun02(B);
  0.111525 seconds (27.23 k allocations: 1.895 MiB, 17.53% compilation time)

@time testfun02(B);
  0.096936 seconds (2 allocations: 234.453 KiB)

```

The version info is

```julia
Julia Version 1.6.2
Commit 1b93d53fc4 (2021-07-14 15:36 UTC)
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-11.0.1 (ORCJIT, skylake)
Environment:
  JULIA_EDITOR = code
  JULIA_NUM_THREADS =

```

and that looks indeed strange.

---

_[View the full topic](https://discourse.julialang.org/t/substituting-elements-of-array-is-very-slow/65036)._
