# Does Julia optimize code automatically?

**URL:** <https://discourse.julialang.org/t/does-julia-optimize-code-automatically/108913>\
**Category:** General Usage\
**Tags:** question\
**Created:** [January 17, 2024, 2:46pm UTC](https://discourse.julialang.org/t/does-julia-optimize-code-automatically/108913 "2024-01-17T14:46:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![MatheusJanczkowski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matheusjanczkowski/32/206380_2.png) [@MatheusJanczkowski](https://discourse.julialang.org/u/MatheusJanczkowski)\
**Post date:** [January 17, 2024, 2:46pm UTC](https://discourse.julialang.org/t/does-julia-optimize-code-automatically/108913/1 "2024-01-17T14:46:53Z")

</div>

I have a piece of code as follows

```
for i=1:n

    b = (M .+d*N)*v + (M*g.+ N*h)*a

end

```

b, v and a are n x 1 vectors; while M and N are n x n matrices. d, g and h are constants that do not change during the loop as well as M and N; the only things that change are v and a.

Does it make any difference in running time precomputing the sum of these matrices (M .+d \* N and M \* g.+ N\*h) out of the for loop and storing them? How does it behaviors when the size of these matrices increases?

The title of this post is due to the fact that I tested both ways and couldn’t find much difference in running time. So does Julia automatically identify this pattern and precomputes such sums?

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [January 17, 2024, 3:00pm UTC](https://discourse.julialang.org/t/does-julia-optimize-code-automatically/108913/2 "2024-01-17T15:00:11Z")

</div>

This is an optimization known as loop-invariant code motion (LICM), which as the name suggests tries to identify code that doesn’t change in the loop and move it out of the loop. Julia’s compiler (and LLVM) sometimes is able to perform this optimization, and newer versions of Julia get better at, but it’s not always reliably able to identify when it can apply it.

So generally it is a good idea to hoist the code out of the loop yourself, especially when it is a relatively expensive calculation.

BTW: [Manual LICM in Julia](https://vchuravy.dev/talks/licm/) has some interesting slides about how LICM can work in the language of the compiler.

See also [Loops, allocations and helping the coder](https://discourse.julialang.org/t/loops-allocations-and-helping-the-coder/104733)

---

<div class="post-metadata">

**Author:** ![MatheusJanczkowski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matheusjanczkowski/32/206380_2.png) [@MatheusJanczkowski](https://discourse.julialang.org/u/MatheusJanczkowski)\
**Post date:** [January 17, 2024, 4:41pm UTC](https://discourse.julialang.org/t/does-julia-optimize-code-automatically/108913/3 "2024-01-17T16:41:04Z")

</div>

Thank you very much for your reply. It explains why I haven’t seen any difference in running time, for Julia must have optimized it automatically. Thanks again!
