# Localizing variables: performance implications?

**URL:** https://discourse.julialang.org/t/localizing-variables-performance-implications/8329
**Category:** Performance
**Created:** [January 12, 2018, 9:30pm UTC](https://discourse.julialang.org/t/localizing-variables-performance-implications/8329 "2018-01-12T21:30:06Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![SepandMeenu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sepandmeenu/32/8846_2.png) [@SepandMeenu](https://discourse.julialang.org/u/SepandMeenu)
#### Post date: [January 12, 2018, 9:30pm UTC](https://discourse.julialang.org/t/localizing-variables-performance-implications/8329/1 "2018-01-12T21:30:06Z")

</div>

Is there any advantage to localizing variables in a Julia function?  
As an example, suppose that `MyStruct` is a `mutable struct` with a field `x`:

```julia
mutable struct MyStruct
     x0
     # ...
end

```

and a function which takes an instance of `MyStruct` as an argument:

```julia
function foo(z::MyStruct)
    #... use z.x0 many times ...
end

```

Is it recommended to “localize” `x0` in `foo` as a constant, as

```julia
function foo(z::MyStruct)
    const x00 = z.x0
    #... use x00 many times ...
end

```

?

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [January 12, 2018, 9:45pm UTC](https://discourse.julialang.org/t/localizing-variables-performance-implications/8329/2 "2018-01-12T21:45:56Z")

</div>

There’s no advantage and not current allowed to make a variable constant. The compiler doesn’t need that information at all.

There can be an advantage to just do `x00 = z.x0`. It may or may not be possible for the compiler to infer that. The effect of that can range from improving the performance by a order of magnitude (e.g. when that helps alias analysis to enable loop vectorization) to slightly negative (e.g. when the increase in register pressure cause additional spilling)

---

<div class="post-metadata">

### Author: ![SepandMeenu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sepandmeenu/32/8846_2.png) [@SepandMeenu](https://discourse.julialang.org/u/SepandMeenu)
#### Post date: [January 16, 2018, 3:16pm UTC](https://discourse.julialang.org/t/localizing-variables-performance-implications/8329/3 "2018-01-16T15:16:27Z")

</div>

You mean that the compiler deduces the ‘constantness’ of a variable automatically in any case, or only if one makes an _explicit_ assignment like `x00 = z.x0`?

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [January 17, 2018, 11:05pm UTC](https://discourse.julialang.org/t/localizing-variables-performance-implications/8329/4 "2018-01-17T23:05:54Z")

</div>

I’m not sure how you make a var without an assignment

---

<div class="post-metadata">

### Author: ![SepandMeenu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sepandmeenu/32/8846_2.png) [@SepandMeenu](https://discourse.julialang.org/u/SepandMeenu)
#### Post date: [January 18, 2018, 8:53am UTC](https://discourse.julialang.org/t/localizing-variables-performance-implications/8329/5 "2018-01-18T08:53:05Z")

</div>

Sorry, I don’t get your question 🙄  
I just need more clarification on your original answer; it’s rather terse and technical.

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [January 18, 2018, 9:02am UTC](https://discourse.julialang.org/t/localizing-variables-performance-implications/8329/6 "2018-01-18T09:02:40Z")

</div>

@yuyichao said that it depends, so best just profile it. 😉

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [January 18, 2018, 11:32am UTC](https://discourse.julialang.org/t/localizing-variables-performance-implications/8329/7 "2018-01-18T11:32:59Z")

</div>

I did not say “it depends” for constantness. I just don’t know what is the difference between `a variable (automatically) in any case` and `an explicit assignment like x00 = z.x0`

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [January 18, 2018, 12:23pm UTC](https://discourse.julialang.org/t/localizing-variables-performance-implications/8329/8 "2018-01-18T12:23:15Z")

</div>

Sorry!
