I am writing Module that calculates thermodynamic properties of water. Module consists of lots of functions and each function requires 34 constant values. Fortunately, those parameters are same for all functions.
Currently, I m declaring those parameters as constant arrays inside the module.
My question is what is the best way of passing constant coefficients into the function in terms of faster computation.
If they are truly module-wide constants that will never be changed by the user, e.g. Boltzmann’s constant, then I would just declare them as global constants in the module? e.g. using
will be just as fast as pasting that literal value into your code.
It only makes sense to put them into an array if you are going to loop over them? If they are unrelated constants, e.g. Boltzmann’s constant, the density of water, and the specific heat of water, then an array makes less sense (and will be slower and more awkward to access).
On the other hand, if they are parameters that are potentially going to be changed by the user, it would make more sense to wrap them into a struct MyParams that you optionally pass into your functions, probably using Base.@kwdef so that the parameters have default values. e.g. you could declare your functions as function foo(args....; params::MyParams=default_params) where default_params is a global constant of default parameter values, so that you can omit the params argument in the common case where you are using the default values.
I thought passing them as a default parameter would be faster in terms of computation. ( I could be wrong, I am new to julia). If there’s no difference, I’ll only use them as global constant.
The advantage of having them as parameters, even if they are truly constant, is that it allows a greater variety of tests, and the possible flexibility of later supporting different units.