# Reduce the allocations in the for loop

**URL:** <https://discourse.julialang.org/t/reduce-the-allocations-in-the-for-loop/106348>\
**Category:** General Usage\
**Tags:** question, memory-allocation\
**Created:** [November 17, 2023, 1:49am UTC](https://discourse.julialang.org/t/reduce-the-allocations-in-the-for-loop/106348 "2023-11-17T01:49:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![supeng\_xu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/supeng_xu/32/47352_2.png) [@supeng\_xu](https://discourse.julialang.org/u/supeng_xu)\
**Post date:** [November 17, 2023, 1:49am UTC](https://discourse.julialang.org/t/reduce-the-allocations-in-the-for-loop/106348/1 "2023-11-17T01:49:33Z")

</div>

With the increase of `Num`, the allocations become larger and larger, can someone help me to optimize this program, here is an easy example  
ps: in my program, Num is quite larger, ~100000

```julia
function test(x,y)
    c=rand(2).*[pi, 2pi]
    x+=c[1]
    y+=c[2]
    return x,y
end

Num=100
a=zeros(Num)
b=ones(Num)

@time begin

@inbounds for ii in 1:Num-1
    global a[ii+1],b[ii+1]=test(a[ii],b[ii])
end

end

```

---

<div class="post-metadata">

**Author:** ![Tarny\_GG\_Channie](https://avatars.discourse-cdn.com/v4/letter/t/3bc359/32.png) [@Tarny\_GG\_Channie](https://discourse.julialang.org/u/Tarny_GG_Channie)\
**Post date:** [November 17, 2023, 2:31am UTC](https://discourse.julialang.org/t/reduce-the-allocations-in-the-for-loop/106348/2 "2023-11-17T02:31:59Z")

</div>

To optimize:  
First, wrap the thing in a function. Accessing a global variable is slowwwwwwww.  
Second, set the an and b to undef and a[1] and b[1] to 0 and 1 respectively.  
Finally, avoid using an array, you can use a pair of variables instead.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [November 17, 2023, 9:20am UTC](https://discourse.julialang.org/t/reduce-the-allocations-in-the-for-loop/106348/3 "2023-11-17T09:20:31Z")

</div>

Read: [Performance Tips · The Julia Language](https://docs.julialang.org/en/v1/manual/performance-tips/)

And probably: [Common allocation mistakes](https://discourse.julialang.org/t/common-allocation-mistakes/66127)

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [November 17, 2023, 3:11pm UTC](https://discourse.julialang.org/t/reduce-the-allocations-in-the-for-loop/106348/4 "2023-11-17T15:11:25Z")

</div>

> [@supeng\_xu](#):
>
> ```julia
> function test(x,y)
> c=rand(2).*[pi, 2pi]
> x+=c[1]
> y+=c[2]
> return x,y
> end
> 
> ```

`rand(2)` will allocate a length=2 array, as will `[pi,2pi]`, and then the product will allocate a third.

Consider using [`StaticArrays`](https://github.com/JuliaArrays/StaticArrays.jl) when you need small, fixed size arrays and you want them to be fast. With `SVector`, this could be non-allocating.

But in this specific case, it’s simple enough to avoid any array at all (although a `SVector` should be equally fast). For example, something like with `test(x,y) = (x + rand()*pi, y + rand()*2pi)`.

Timing the loop in global scope will lead to many allocations. See the link to the performance tips from a previous post.
