Increase the speed of For Loop in julia

Did you try to profile it? Where is most of the time spent? Also, please provide enough info so that the problem can be reproduced. For example, what are the input values for the functions that you use?

One things I can see is:

   @inbounds for ecp in 1:4
                dy[icount] += -factor * s[jj] * (ft[jj, ga, ecp] * conj(y[ge[gb, ecp]]) - ftc[jj, gb, ecp] * y[ge[ga, ecp]])
            end

Looking at ft[jj, ga, ecp] you are looping in the wrong order. You want to loop over the firstmost index the fastest. See the performance tips: Performance Tips · The Julia Language

So you want it to be:

@inbounds for jj in 1:Len
    for ga in 1:12, gb in ga:12
        for ecp in 1:4
            ... ft[ecp, ga, jj] ...
        end
    end
end

Also, you don’t need to put @inbounds on all the inner loops, it is enough to put it on the most outer one.

2 Likes