Substituting elements of array is very slow

Also, here are some benchmarks using BenchmarkTools
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.                               
1 Like