# Large memory allocation in function within function

**URL:** <https://discourse.julialang.org/t/large-memory-allocation-in-function-within-function/26229>\
**Category:** New to Julia\
**Tags:** memory-allocation\
**Created:** [July 10, 2019, 5:06pm UTC](https://discourse.julialang.org/t/large-memory-allocation-in-function-within-function/26229 "2019-07-10T17:06:02Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![mrVeng](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrveng/32/8836_2.png) [@mrVeng](https://discourse.julialang.org/u/mrVeng)\
**Post date:** [July 11, 2019, 6:24am UTC](https://discourse.julialang.org/t/large-memory-allocation-in-function-within-function/26229/4 "2019-07-11T06:24:29Z")

</div>

Thank you for your answer!

I had a look at it and redefined fun3 the following way below. That way, memory allocation is roughly the one from fun1 + fun2 (expected), the computational time is still roughly 1.5 times higher than the individual times of fun1 + fun2.

```julia
function fun4(mystruct::Supertype, observations::AbstractArray)

log_evidence = fun1(MyObject, obs)::Array{Float64,2} #NEW function barrier
alpha = zeros( size(log_evidence) )

    for t in 1:size(log_evidence)[1]
        alpha[t,:] = log_evidence[t,:]
    end
end

@benchmark fun1(MyObject, obs)
@benchmark fun2(MyObject, log_evidence)
@benchmark fun4(MyObject, obs)

```

(1) Is there a better way to trim that down/use function barries? I tried initializing an empty array and then perform the computation already, but it didnt really improve things (I guess it just overrode the empty array when I assigned the function to the variable). Allocating an Matrix with a Matrix seems to be not working for me:

```julia
log_evidence = fun1(MyObject, obs)::Array{Float64,2}
alpha = zeros( size(log_evidence) )

typeof(log_evidence) #Matrix{Float64}
typeof(alpha) #Matrix{Float64}

#not working
fill!(alpha, log_evidence)

```

Do I need to initialize an empty array and then fill it rowwise with a for loop?

(2) Also, if I have a function with only 1 input that varies, and that input has the same type for all different variations, then i guess I cannot use Multiple dispatch? In this example that would be:

```julia
function fun2(mystruct::Supertype, observations::AbstractArray)
#do computations as above
end
function fun2(mystruct::Supertype, log_evidence::AbstractArray)
#do computations as above
end

```

What I could do is probably define a custom type for `log_evidence`?

Thank you, again, for your time!

Best regards,  
###############################################################################

Edit:  
For large T, the difference seems to vanish and the computation time is roughly the same as its individual parts. I take it that I probably should have used

```julia
function fun1(mystruct::Supertype, observations::AbstractArray)::Array{Float64,2}
    hcat(map(distribution -> logpdf.(distribution, observations), mystruct.distr)...)
end

```

straight so I wont need to that within other functions.

---

_[View the full topic](https://discourse.julialang.org/t/large-memory-allocation-in-function-within-function/26229)._
