# Pre-allocating Arrays for intermediate calculations

**URL:** <https://discourse.julialang.org/t/pre-allocating-arrays-for-intermediate-calculations/21725>\
**Category:** General Usage\
**Created:** [March 11, 2019, 7:21am UTC](https://discourse.julialang.org/t/pre-allocating-arrays-for-intermediate-calculations/21725 "2019-03-11T07:21:32Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![krenova](https://avatars.discourse-cdn.com/v4/letter/k/53a042/32.png) [@krenova](https://discourse.julialang.org/u/krenova)\
**Post date:** [March 11, 2019, 7:21am UTC](https://discourse.julialang.org/t/pre-allocating-arrays-for-intermediate-calculations/21725/1 "2019-03-11T07:21:32Z")

</div>

To save on computation time for a function i often use, i created a preallocated array that i pass to the function to store intermediate calculations. As an example, albeit a contrived one, i have this function test() that I often need to use:

```julia
# Let N be some number representing some integer representing the array size
_b = Array{Float64,1}(undef,N)
function test( a, b = _b)
     for i in 1:100 
          b[i] += a + i
     end
     return( b .* 100 )
end

```

In the example, i created a preallocated array and use it to store intermediate results in the hope to save on computation time. However, I wonder if this makes sense, if it could cause some unforeseen bugs later on and if there are better ways to go about reducing computation time.

Please advise.

‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’’  
Please note that i meant `b[i] = a + i`. Which is very different from `b[i] += a + i`. Sorry for the confusion. The responses below still hold. thanks for pointing out @DNF.

---

<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:** [March 11, 2019, 7:26am UTC](https://discourse.julialang.org/t/pre-allocating-arrays-for-intermediate-calculations/21725/2 "2019-03-11T07:26:16Z")

</div>

Rather than using global variables, it is often better to let the caller preallocate the workspace needed. The caller can then hoist that preallocation outside of the loop.

---

<div class="post-metadata">

**Author:** ![krenova](https://avatars.discourse-cdn.com/v4/letter/k/53a042/32.png) [@krenova](https://discourse.julialang.org/u/krenova)\
**Post date:** [March 11, 2019, 7:37am UTC](https://discourse.julialang.org/t/pre-allocating-arrays-for-intermediate-calculations/21725/3 "2019-03-11T07:37:32Z")

</div>

Hi @kristoffer.carlsson, thanks for your prompt response. Do you mean that I do not have to worry about preallocation within functions as the function will automatically do a preallocation? In the context of the given example, it means the object b, once created, will always exists till the programme stops?

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [March 11, 2019, 8:04am UTC](https://discourse.julialang.org/t/pre-allocating-arrays-for-intermediate-calculations/21725/4 "2019-03-11T08:04:54Z")

</div>

No, more like

```julia

function test!( b, a)
    @inbounds @simd for i in 1:100 
        b[i] += a + i
    end
    # Why not reuse "b" again?
    b .*= 100
end

function test_a_lot(N)
    _b = Array{Float64,1}(undef, 100)
    result = 0.0
    for n ∈ 1:N
        result += (-0.01)^n * sum(test!(_b, 0.5n))
    end
    result
end

```

That is, whatever function you’re using that is using `test` many times should manage preallocating the buffer.  
If you’re only using test once, there isn’t going to be any benefit.

If you’re using it many times from many different places, maybe you can pass around a program state object that stores all the state and buffer variables, so you only need to manage a single object.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [March 11, 2019, 8:19am UTC](https://discourse.julialang.org/t/pre-allocating-arrays-for-intermediate-calculations/21725/5 "2019-03-11T08:19:35Z")

</div>

> [@krenova](#):
>
> `return( b .* 100 )`

It’s a bit odd to go to the trouble of pre-allocating, only to throw away the benefit and make a new vector in the return statement…

You should definitely be careful with globals.

`_b` will be mutated each time you call `test`, so the returned result will be different each time, even with the same input. Is that intentional? And using `undef` and then incrementing, means that the output may contain garbage. Is that intentional too?

PS. Minor point: `return` is a keyword not a function, so it’s idiomatic to not use parens: `return b .* 100`

---

<div class="post-metadata">

**Author:** ![tkluck](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkluck/32/15769_2.png) [@tkluck](https://discourse.julialang.org/u/tkluck)\
**Post date:** [March 11, 2019, 7:47pm UTC](https://discourse.julialang.org/t/pre-allocating-arrays-for-intermediate-calculations/21725/6 "2019-03-11T19:47:36Z")

</div>

Maybe capturing into a closure is the pattern you are looking for. Here’s what that could look like (with a similarly contrived example):

```julia
julia> f = let b=Vector{Int}(undef, 1000)
           function f(x)
              b .= (1:1000) .+ x
              return sum(b)
           end
       end
f (generic function with 1 method)

julia> f(3)
503500

```

I think this expresses what you want to do in the nicest way. Depending on your use case, it does have a performance downside: [Performance Tips · The Julia Language](https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-captured-1) . Do benchmark your code to check.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [March 12, 2019, 7:29am UTC](https://discourse.julialang.org/t/pre-allocating-arrays-for-intermediate-calculations/21725/7 "2019-03-12T07:29:16Z")

</div>

> [@tkluck](#):
>
> I think this expresses what you want to do in the nicest way.

I would argue that in most cases, defining a `struct` that holds the buffer and possibly making it callable is a better solution — it provides better introspection and maintainability, and of course you are less likely to run into the above bug.

---

<div class="post-metadata">

**Author:** ![krenova](https://avatars.discourse-cdn.com/v4/letter/k/53a042/32.png) [@krenova](https://discourse.julialang.org/u/krenova)\
**Post date:** [March 12, 2019, 7:45am UTC](https://discourse.julialang.org/t/pre-allocating-arrays-for-intermediate-calculations/21725/8 "2019-03-12T07:45:25Z")

</div>

thanks @tkluck, this does indeed reflect what I hope to do. But as you cautioned, it could result in performance issues. Will keep that in mind.

---

<div class="post-metadata">

**Author:** ![krenova](https://avatars.discourse-cdn.com/v4/letter/k/53a042/32.png) [@krenova](https://discourse.julialang.org/u/krenova)\
**Post date:** [March 12, 2019, 7:49am UTC](https://discourse.julialang.org/t/pre-allocating-arrays-for-intermediate-calculations/21725/9 "2019-03-12T07:49:45Z")

</div>

thanks @Tamas_Papp, do you mean that I should define a `struct` that holds these interim objects that i can call when needed? Hope its not a stupid question, I code predominantly in R and dont usually find myself having to deal with these different types of programming tools.

---

<div class="post-metadata">

**Author:** ![krenova](https://avatars.discourse-cdn.com/v4/letter/k/53a042/32.png) [@krenova](https://discourse.julialang.org/u/krenova)\
**Post date:** [March 12, 2019, 7:52am UTC](https://discourse.julialang.org/t/pre-allocating-arrays-for-intermediate-calculations/21725/10 "2019-03-12T07:52:39Z")

</div>

thanks for pointing out @DNF, it should have been `b[i] = a + i` in the example.

Also thanks for pointing out that parentheses are not necessary for the keyword `return`

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [March 12, 2019, 8:07am UTC](https://discourse.julialang.org/t/pre-allocating-arrays-for-intermediate-calculations/21725/11 "2019-03-12T08:07:43Z")

</div>

maybe an example helps:

```julia
struct BufferedContrivedExample{T}
    buffer::T
end

BufferedContrivedExample(T, N::Integer) = BufferedContrivedExample(Vector{T}(undef, N))

function (bce::BufferedContrivedExample)(a)
    b = bce.buffer
    for i in axes(b, 1)
        b[i] += a + i
    end
    b .* 100
end

bce = BufferedContrivedExample(Float64, 100);

bce(1.0)

```
