Suppose I have a model M
with parameters θ
that are arranged in a ComponentArray
. Model M
has several components M1
to M2
that each take a subset of components of θ
. I’m wondering what’s better, balancing performance and code organization/readability, of the options below (this is toy example, but suppose that all functions are costly).
using ComponentArrays
using UnPack
θ = ComponentArray{Float64}(θ1 = rand(1000), θ2 = rand(500))
function F(m1, m2)
m1 + m2
end
## Option A
function AM1(θ1)
prod(θ1)
end
function AM2(θ2)
prod(sin.(θ2))
end
function AM(θ)
@unpack θ1, θ2 = θ
m1 = AM1(θ1)
m2 = AM2(θ2)
F(m1, m2)
end
## Option B
function BM1(θ)
@unpack θ1 = θ
prod(θ1)
end
function BM2(θ)
@unpack θ2 = θ
prod(sin.(θ2))
end
function BM(θ)
m1 = BM1(θ)
m2 = BM2(θ)
F(m1, m2)
end