# Increase the speed of For Loop in julia

**URL:** <https://discourse.julialang.org/t/increase-the-speed-of-for-loop-in-julia/89985>\
**Category:** General Usage\
**Tags:** question\
**Created:** [November 9, 2022, 2:07pm UTC](https://discourse.julialang.org/t/increase-the-speed-of-for-loop-in-julia/89985 "2022-11-09T14:07:03Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [November 9, 2022, 2:34pm UTC](https://discourse.julialang.org/t/increase-the-speed-of-for-loop-in-julia/89985/2 "2022-11-09T14:34:16Z")

</div>

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:

```julia
   @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](https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-column-major)

So you want it to be:

```julia
@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.

---

_[View the full topic](https://discourse.julialang.org/t/increase-the-speed-of-for-loop-in-julia/89985)._
