# 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:** 10

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [July 21, 2021, 5:20pm UTC](https://discourse.julialang.org/t/substituting-elements-of-array-is-very-slow/65036/10 "2021-07-21T17:20:44Z")

</div>

> **Also, here are some benchmarks using BenchmarkTools**
>
> ```julia
> julia> using LinearAlgebra                                                      
>                                                                                 
> julia> using BenchmarkTools                                                     
>                                                                                 
> julia> function testfun02(M)                                                    
> nrow, ncol = size(M);                                                
> spidx = Array{Float64, 2}(undef, nrow, 3);                           
> numit = 1;                                                           
>                                                                                 
> for j02 = 1:ncol, j01 in 1:nrow                                      
> aval = M[j01,j02]*(j01+j02)                                      
> end                                                                  
>                                                                                 
> return spidx                                                         
> end                                                                      
> testfun02 (generic function with 1 method)                                      
>                                                                                 
> julia> function testfun03(M)                                                    
> nrow, ncol = size(M);                                                
> spidx = Array{Float64, 2}(undef, nrow, 3);                           
> numit = 1;                                                           
>                                                                                 
> for j02 = 1:ncol, j01 in 1:nrow                                      
> aval = M[j01,j02]*(j01+j02);                                     
> if aval > 0;                                                     
> spidx[numit,1] = j01;                                        
> spidx[numit,2] = j02;                                        
> spidx[numit,3] = aval;                                       
> numit +=1;                                                   
> end                                                              
> end                                                                  
> return(spidx);                                                       
> end                                                                      
> testfun03 (generic function with 1 method)                                      
>                                                                                 
> julia> @benchmark testfun02(B) setup=(B=Matrix{Float64}(I, 10000,10000)) evals=1
> BechmarkTools.Trial: 5 samples with 1 evaluations.                              
> Range (min … max): 6.300 μs … 41.900 μs ┊ GC (min … max): 0.00% … 0.00%     
> Time (median): 13.500 μs ┊ GC (median): 0.00%             
> Time (mean ± σ): 16.880 μs ± 14.626 μs ┊ GC (mean ± σ): 0.00% ± 0.00%     
>                                                                                 
> █ ▁ ▁ ▁▁                  
> █▁▁▁▁▁▁▁▁▁▁█▁▁▁▁█▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁█ ▁                 
> 6.3 μs Histogram: frequency by time 41.9 μs <                 
>                                                                                 
> Memory estimate: 234.42 KiB, allocs estimate: 2.                               
>                                                                                 
> julia> @benchmark testfun03(B) setup=(B=Matrix{Float64}(I, 10000,10000)) evals=1
> BechmarkTools.Trial: 4 samples with 1 evaluations.                              
> Range (min … max): 105.353 ms … 108.477 ms ┊ GC (min … max): 0.00% … 0.00%   
> Time (median): 107.536 ms ┊ GC (median): 0.00%           
> Time (mean ± σ): 107.225 ms ± 1.344 ms ┊ GC (mean ± σ): 0.00% ± 0.00%   
>                                                                                 
> █ █ █ ██                
> █▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁█▁▁▁▁▁▁▁▁▁▁█▁▁▁▁▁▁▁▁▁▁▁█ ▁               
> 105 ms Histogram: frequency by time 108 ms <               
>                                                                                 
> Memory estimate: 234.42 KiB, allocs estimate: 2.                               
> 
> ```

---

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