January 2020
Intermediate to advanced
532 pages
13h 31m
English
There is another way to tackle the problem of global variables. In a performance-sensitive function, rather than accessing the global variable directly, we can pass the global variable into the function as an argument.
Let's refactor the code earlier in this section by adding a second argument, as follows:
function add_by_passing_global_variable(x, v) return x + vend
Now, we can call the function by passing in the variable. Let's benchmark the code as follows:

Fantastic! It's as fast as treating it as a constant. Where's the magic? As it turns out, Julia's compiler automatically generates ...
Read now
Unlock full access