# Nested Loop optimization

**URL:** https://discourse.julialang.org/t/nested-loop-optimization/87498
**Category:** Performance
**Created:** [September 19, 2022, 11:05pm UTC](https://discourse.julialang.org/t/nested-loop-optimization/87498 "2022-09-19T23:05:01Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Jojo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jojo/32/42799_2.png) [@Jojo](https://discourse.julialang.org/u/Jojo)
#### Post date: [September 19, 2022, 11:05pm UTC](https://discourse.julialang.org/t/nested-loop-optimization/87498/1 "2022-09-19T23:05:01Z")

</div>

Hello, I’ve been working on a solver where I have a nested loop and I want to improve the performance. I think that I’m making unnecessary allocations, for instance I have to write a=copy(a) to force the inner loop to update “a”. Any general tip on how to improve the performance of the loop or how to get rid of copying “a” every j iteration would be appreciated.

P.D I feed the function with arrays of length(Nx) then II fix the a boundary values in every step. My goal is to modify “a” with the inner loop without losing performance. For reference, I’m working in a Nx=12000,Nt=40000 grid.

This is a simplified version of the loop I use in my solver:

```julia

function TXloop(a,b,c)
   for j = 2:Nt
      c = b
      b = a
      a=copy(a)
      @avxt for i = 2:Nx-1
         a[i] =b[i]+c[i]
         end
      a[Nx]=c[1]
      a[1]=c[2]-c[1]
      push!(m, a)
    end
   end

```

---

<div class="post-metadata">

### Author: ![jmair](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmair/32/35117_2.png) [@jmair](https://discourse.julialang.org/u/jmair)
#### Post date: [September 19, 2022, 11:24pm UTC](https://discourse.julialang.org/t/nested-loop-optimization/87498/2 "2022-09-19T23:24:35Z")

</div>

`copy` and `push!` will both allocate, and usually you can avoid this by preallocating your memory. Work out how much memory you need first and create an array of that type. A crude example:

```julia
function TXloop(a,b,c)
   # preallocate arrays at the start
   a_buffer = similar(a)
   m = zeros(eltype(a), Nt-1,length(a))
   for j = 2:Nt
      # same as copy, but doesn't allocate
      a_buffer .= a
      # calculate something

     # copy into a section of m
      m[j-1, :] .= a_buffer
    end
end

```

The `.=` operator is your friend when working with arrays as it broadcasts element-wise, and anything on the right hand side is fused to avoid allocating arrays for intermediate results. There’s also no reason you couldn’t also use a for loop.

Look for methods with a `!` at the end of the name as these are mutating methods that alter the first argument, which usually let you do operations on some preallocated memory.

---

<div class="post-metadata">

### Author: ![Jojo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jojo/32/42799_2.png) [@Jojo](https://discourse.julialang.org/u/Jojo)
#### Post date: [September 20, 2022, 12:01am UTC](https://discourse.julialang.org/t/nested-loop-optimization/87498/3 "2022-09-20T00:01:50Z")

</div>

Thanks, this is what I was trying to understand. I’ll implement your tips on my routine and see how it goes.
