# Reduce the allocation to zero

**URL:** <https://discourse.julialang.org/t/reduce-the-allocation-to-zero/79160>\
**Category:** General Usage\
**Tags:** question\
**Created:** [April 7, 2022, 12:04pm UTC](https://discourse.julialang.org/t/reduce-the-allocation-to-zero/79160 "2022-04-07T12:04:52Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![xspeng](https://avatars.discourse-cdn.com/v4/letter/x/90ced4/32.png) [@xspeng](https://discourse.julialang.org/u/xspeng)\
**Post date:** [April 7, 2022, 12:04pm UTC](https://discourse.julialang.org/t/reduce-the-allocation-to-zero/79160/1 "2022-04-07T12:04:52Z")

</div>

I have a question about whether it’s possible to reduce the following simple case allocation to zero

```julia
a = rand(5)
b = rand(5)
c = rand(5)
d = rand(5)
N = rand(8)

@time acl = sum(a) * (N[1] - N[5]) + sum(b) * (N[2] - N[6]) + sum(c) * (N[3] - N[7]) + sum(d) * (N[4] - N[8]) # in my computer, the allocation is 21 after compilation, which is unexpectedly high

```

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [April 7, 2022, 12:10pm UTC](https://discourse.julialang.org/t/reduce-the-allocation-to-zero/79160/2 "2022-04-07T12:10:23Z")

</div>

> [@xspeng](#):
>
> ```julia
> a = rand(5)
> b = rand(5)
> c = rand(5)
> d = rand(5)
> N = rand(8)
> 
> @time acl = sum(a) * (N[1] - N[5]) + sum(b) * (N[2] - N[6]) + sum(c) * (N[3] - N[7]) + sum(d) * (N[4] - N[8]) 
> 
> ```

Recommended profiling setup is something like

```julia
using BenchmarkTools

@btime acl = sum(a) * (N[1] - N[5]) + sum(b) * (N[2] - N[6]) + sum(c) * (N[3] - N[7]) + sum(d) * (N[4] - N[8]) setup=(
    a = rand(5);
    b = rand(5);
    c = rand(5);
    d = rand(5);
    N = rand(8);   
)

```

(you are measuring in global scope with non-`const`s: recipe for ‘disaster’).

---

<div class="post-metadata">

**Author:** ![xspeng](https://avatars.discourse-cdn.com/v4/letter/x/90ced4/32.png) [@xspeng](https://discourse.julialang.org/u/xspeng)\
**Post date:** [April 7, 2022, 12:18pm UTC](https://discourse.julialang.org/t/reduce-the-allocation-to-zero/79160/3 "2022-04-07T12:18:15Z")

</div>

So, you mean in reality the result from @time is not true or why it show so high outputs?

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [April 7, 2022, 12:20pm UTC](https://discourse.julialang.org/t/reduce-the-allocation-to-zero/79160/4 "2022-04-07T12:20:16Z")

</div>

The results are true for what you are doing but not representative for what can be done with Julia.

Please see the first two sections in the [Performance Tips](https://docs.julialang.org/en/v1/manual/performance-tips/).

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [April 7, 2022, 12:25pm UTC](https://discourse.julialang.org/t/reduce-the-allocation-to-zero/79160/5 "2022-04-07T12:25:23Z")

</div>

> [@xspeng](#):
>
> ```julia
> a = rand(5)
> b = rand(5)
> c = rand(5)
> d = rand(5)
> N = rand(8)
> 
> @time acl = sum(a) * (N[1] - N[5]) + sum(b) * (N[2] - N[6]) + sum(c) * (N[3] - N[7]) + sum(d) * (N[4] - N[8]) 
> 
> ```

`sum(rand(5))` is equivalent to `sum(_ -> rand(), 1:5)` so your code is equivalent to the following code which uses no arrays:

```julia
sumrand(n) = sum(_ -> rand(), 1:n)
acl = sumrand(5) * (rand() - rand()) + sumrand(5) * (rand() - rand()) + sumrand(5) * (rand() - rand()) + sumrand(5) * (rand() - rand()) 

```

which is allocation-free:

```julia
julia> using BenchmarkTools

julia> @btime acl = sumrand(5) * (rand() - rand()) + sumrand(5) * (rand() - rand()) + sumrand(5) * (rand() - rand()) + sumrand(5) * (rand() - rand());
  68.299 ns (0 allocations: 0 bytes)

```

You could even do

```julia
acl = sum(_ -> sumrand(5) * (rand() - rand()), 1:5)

```

This will show allocations, but only [because it is in global scope](https://docs.julialang.org/en/v1/manual/performance-tips/#Performance-critical-code-should-be-inside-a-function); in a function it will be allocation-free a well:

```julia
julia> f(n, m) = sum(_ -> sumrand(m) * (rand() - rand()), 1:n)
f (generic function with 1 method)

julia> @btime f(5,5);
  102.041 ns (0 allocations: 0 bytes)

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [April 7, 2022, 12:34pm UTC](https://discourse.julialang.org/t/reduce-the-allocation-to-zero/79160/6 "2022-04-07T12:34:41Z")

</div>

> [@stevengj](#):
>
> `sum(rand(5))` is equivalent to `sum(_ -> rand(), 1:5)` so your code is equivalent to the following code which uses no arrays:

But maybe I misunderstood your question, and you are only asking about reducing the allocations in the `acl = ...` line. In that case, the problem is simply that you are benchmarking using global variables as noted above.

---

<div class="post-metadata">

**Author:** ![xspeng](https://avatars.discourse-cdn.com/v4/letter/x/90ced4/32.png) [@xspeng](https://discourse.julialang.org/u/xspeng)\
**Post date:** [April 7, 2022, 12:52pm UTC](https://discourse.julialang.org/t/reduce-the-allocation-to-zero/79160/7 "2022-04-07T12:52:04Z")

</div>

Thanks for your detailed explanation, so if I understand correct, it’s better to put all the formula within a function, right?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [April 7, 2022, 1:07pm UTC](https://discourse.julialang.org/t/reduce-the-allocation-to-zero/79160/8 "2022-04-07T13:07:29Z")

</div>

Yes. Read the [Performance Tips](https://docs.julialang.org/en/v1/manual/performance-tips/) in the manual, especially the first two tips.
