# Julia in-place modification and memory usages

**URL:** <https://discourse.julialang.org/t/julia-in-place-modification-and-memory-usages/40962>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [June 8, 2020, 1:33am UTC](https://discourse.julialang.org/t/julia-in-place-modification-and-memory-usages/40962 "2020-06-08T01:33:57Z")\
**Posts on this page:** 2\
**Page:** 2

<div class="post-metadata">

**Author:** ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)\
**Post date:** [June 8, 2020, 12:09pm UTC](https://discourse.julialang.org/t/julia-in-place-modification-and-memory-usages/40962/21 "2020-06-08T12:09:31Z")

</div>

The problem with global variables is that the compiler cannot be sure that their type does not change. Therefore, they need to be “boxed”, i.e. their type must be checked at run-time for each operation on them. If they are used in tight loops, this could easily result in a performance loss of a factor 100.  
Putting them into a module does not help if they are still (module-level) globals.  
To avoid the performance penalty, you could either declare the globals as `const` or pass them as function arguments. For a large number of variables, you could either use NamedTuples (as @mcabbott suggested) or a custom struct.  
For the latter, you can define default values using `Base.@kwdef`.  
When defining a struct, make sure to use concrete or parametric types, not abstract types for the same performance reasons as described above.

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [June 8, 2020, 3:11pm UTC](https://discourse.julialang.org/t/julia-in-place-modification-and-memory-usages/40962/22 "2020-06-08T15:11:08Z")

</div>

If the global variables are constant, I.e. you never need to change them, you can mark them as constant, like `const x = 1`. This thing won’t have any performance impact inside functions.

[Previous page](https://discourse.julialang.org/t/julia-in-place-modification-and-memory-usages/40962.md?page=1)
