Hi, I am beginning to learn how to build my own types, and pass them to functions with different methods for different input types. Below is a chemistry example problem. I do not understand one of the allocations involved.
abstract type RateParams end
struct Reaction
rateparams::RateParams
end
struct elementary_reaction_rate_params <: RateParams
rate::Array{Float64,1}
end
function reaction_rate!(param::elementary_reaction_rate_params,k::Array{Float64,1})
for i in 1:10
k[i] = param.rate[1]*param.rate[2]*param.rate[3]
end
nothing
end
rx1 = Reaction(elementary_reaction_rate_params([1.0,2.0,3.0]))
@time k = zeros(Float64,10)
@time rateparams = rx1.rateparams
@time reaction_rate!(rateparams,k)
This returns
0.000002 seconds (1 allocation: 160 bytes)
0.000005 seconds (1 allocation: 16 bytes)
0.000001 seconds
The first allocation for k
makes sense. But why is there 1 allocation for the line rateparams = rx1.rateparams
? Is there a way to get rid of this allocation? Any other relevant advice is appreciated. Thanks!