Skip to content

Instantly share code, notes, and snippets.

View anicusan's full-sized avatar

Andrei Leonard Nicusan anicusan

View GitHub Profile
@flcong
flcong / GlobalVariablesJulia.md
Last active June 16, 2024 01:35
Global Variable in Julia: How to avoid using it?

Global Variable in Julia: How to avoid using it?

Recently, I tried to rewrite a MATLAB program in Julia. The program solves a PDE derived from a continuous-time economic model. I got the same result as the MATLAB program, but it was much slower. Then, I reviewed the Performance Tips of Julia and realized that the problem lied in using global variables. Typically, there are a lot of parameters in an economic model and they are typically directly defined as global variables. Then, for convenience, I wrote several functions to calculate some formulae which use these parameters. Since those functions were frequently called in a long loop, the performance is low.

To guide future programming practice, here I experiment several ways to avoid this problem.

Performance with/without global variables

Before digging into various ways to avoid this problem, let's first check how slow using global variables can be. To compare the computational time of di