# Using module variables in a module function. Is there a performance disadvantage?

**URL:** <https://discourse.julialang.org/t/using-module-variables-in-a-module-function-is-there-a-performance-disadvantage/42465>\
**Category:** Performance\
**Tags:** question\
**Created:** [July 3, 2020, 9:06am UTC](https://discourse.julialang.org/t/using-module-variables-in-a-module-function-is-there-a-performance-disadvantage/42465 "2020-07-03T09:06:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)\
**Post date:** [July 3, 2020, 9:06am UTC](https://discourse.julialang.org/t/using-module-variables-in-a-module-function-is-there-a-performance-disadvantage/42465/1 "2020-07-03T09:06:30Z")

</div>

```julia
module A

b = "test"

function crazy_use_of_b_1() 
    #... using A.b 
end

function crazy_use_of_b_2(;_b = A.b) 
    #... using _b 
end

end

```

Is there a performance difference from 1 to 2. Or problems with type stability? Which one of the two are better?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [July 3, 2020, 9:36am UTC](https://discourse.julialang.org/t/using-module-variables-in-a-module-function-is-there-a-performance-disadvantage/42465/2 "2020-07-03T09:36:49Z")

</div>

You will be running into

[https://docs.julialang.org/en/v1/manual/performance-tips/#Avoid-global-variables-1](https://docs.julialang.org/en/v1/manual/performance-tips/#Avoid-global-variables-1)

here, unrelated to modules. If you fix that, `A.b` will be fine, though unnecessary within `A`.

Using global _constants_ is not crazy at all, it is part of your programmer’s toolbox.

---

<div class="post-metadata">

**Author:** ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)\
**Post date:** [July 3, 2020, 11:22am UTC](https://discourse.julialang.org/t/using-module-variables-in-a-module-function-is-there-a-performance-disadvantage/42465/3 "2020-07-03T11:22:21Z")

</div>

That’s a need trick.

```julia
global x = rand(1000)

function loop_over_global()
    s = 0.0
    for i in x::Vector{Float64}
        s += i
    end
    return s
end

```

So the code above would be as good as

```julia
global x = rand(1000)

function loop_over_global(_x::Vector{Float64})
    s = zero(eltype(_x))
    for i in _x
        s += i
    end
    return s
end

```

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [July 3, 2020, 12:18pm UTC](https://discourse.julialang.org/t/using-module-variables-in-a-module-function-is-there-a-performance-disadvantage/42465/4 "2020-07-03T12:18:41Z")

</div>

I would do

```julia
const x = rand(1000)

```

If possible. Using this will be type stable.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [July 3, 2020, 12:56pm UTC](https://discourse.julialang.org/t/using-module-variables-in-a-module-function-is-there-a-performance-disadvantage/42465/5 "2020-07-03T12:56:30Z")

</div>

Are you sure you want `s = zero(eltype(x))` and not `s = zero(eltype(_x))`?
