Type inference in Julia primarily works by inspecting the types of function parameters and identifying the type of the return value. This suggests that some type instability issues may be mitigated by breaking up a function into smaller functions. This can provide additional hints to the compiler, making more accurate type inferencing possible.
For an example of this, consider a contrived function that takes as input the "Int64"
or "Float64"
string and returns an array of 10 elements, the types of which correspond to the type name passed as the input argument. Functions such as this may arise when creating arrays based on user input or by reading a file in which the type of the output is determined at runtime. Take a look at the following:
function string_zeros(s::AbstractString) x = Array(s=="Int64"?Int64:Float64, 1_000_000) for i in 1:length(x) x[i] = 0 end return x end
We will benchmark this code to find an average...