It is best when we can just use global constants. But what if the variable does need to be changed during the life cycle of the application? For example, maybe it is a global counter that keeps track of the number of visitors on a website.
At first, we may be tempted to do the following, but we quickly realized that Julia does not support annotating global variables with type information:
Instead, what we can do is to annotate the variable type within the function itself, as follows:
function add_using_global_variable_typed(x)
return x + variable::Int
end
Let's see how it performs:
That's quite a speed boost compared to the untyped version of 31 ns! However, it is still far away from the global constant solution.