Hello. This is my first time asking a question, so I apologize if it’s not in any kind of form. I’m also new to programming and Julia.

As the title says, I often code by indexing array data and declaring it as a new variable. This is normally fine, but when using the Base.Threads library, it causes a serious performance disruption (hundreds of times slower).

This code is a simple test; in practice, it’s much more complex.

Using the “#good” annotated coding style doesn’t degrade performance, but it makes the code very long and less readable. Why is this happening and what is the best way to code so that the code is both readable and performance?

```
struct infomations
nn :: Int64
A1 :: Matrix{Float64}
A2 :: Matrix{Float64}
dx :: Float64
dy :: Float64
function infomations(nn,L)
nn = nn+4
A1 = zeros(nn,nn)
A2 = rand(nn,nn)
dx = L/nn
dy = L/nn
new(nn,A1,A2,dx,dy)
end
end
function get_Div(obj1)
(;nn, A1, A2, dx, dy) = obj1
ny,nx = size(A1)
Threads.@threads for i = 3:nx-2
for j = 3:ny-2
# bad
ee = A2[j,i+2]; e = A2[j,i+1]; w = A2[j,i-1]; ww = A2[j,i-2]
nn = A2[j+2,i]; n = A2[j+1,i]; s = A2[j-1,i]; ss= A2[j-2,i]
∂B1∂x = (-ee + 8*e - 8*w + ww)/dx/12
∂B2∂y = (-nn + 8*n - 8*s + ss)/dy/12
A1[j,i] = ∂B1∂x + ∂B2∂y
# good
∂B1∂x = (-A2[j,i+2] + 8*A2[j,i+1] - 8*A2[j,i-1] + A2[j,i-2])/dx/12
∂B2∂y = (-A2[j+2,i] + 8*A2[j+1,i] - 8*A2[j-1,i] + A2[j-2,i])/dy/12
A1[j,i] = ∂B1∂x + ∂B2∂y
end
end
end
function run!(obj1, iter)
for i = 1:iter
get_Div(obj1)
end
end
obj1 = infomations(2000,3)
@time run!(obj1, 200)
```