# Looking for Some Best Practices for Optimizing Julia Code Performance?

**URL:** <https://discourse.julialang.org/t/looking-for-some-best-practices-for-optimizing-julia-code-performance/124117>\
**Category:** General Usage\
**Created:** [December 23, 2024, 11:28am UTC](https://discourse.julialang.org/t/looking-for-some-best-practices-for-optimizing-julia-code-performance/124117 "2024-12-23T11:28:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielljose](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielljose/32/214353_2.png) [@danielljose](https://discourse.julialang.org/u/danielljose)\
**Post date:** [December 23, 2024, 11:28am UTC](https://discourse.julialang.org/t/looking-for-some-best-practices-for-optimizing-julia-code-performance/124117/1 "2024-12-23T11:28:07Z")

</div>

Hi everyone,

I am new to Julia and absolutely loving its speed and simplicity so far. However, as I start working on more complex projects…, I would like to ensure my code is as efficient as possible. I have read about Julia’s impressive performance potential, but I am aware that achieving optimal performance often requires a deep understanding of the language’s best practices.

Type Stability: How can I consistently ensure my functions remain type-stable? Any quick debugging tips?  
Memory Allocation: Are there tools or workflows for spotting and reducing unnecessary memory allocations?  
Parallelization: What’s the best way to approach parallel computing in Julia for someone coming from Python?  
Profiling Tools: Are there community-recommended tools for profiling and visualizing performance bottlenecks?

I would greatly appreciate any advice, examples or links to resources that can help me build better habits in writing high-performance Julia code.

Thanks in advance !!

Looking forward to learning from this awesome community !!

With Regards  
Daniel Jose

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [December 23, 2024, 11:31am UTC](https://discourse.julialang.org/t/looking-for-some-best-practices-for-optimizing-julia-code-performance/124117/2 "2024-12-23T11:31:35Z")

</div>

Understand the programming model and this information comes for free. I recommend some parts of the SciML course on this, see:

[https://book.sciml.ai/notes/02-Optimizing\_Serial\_Code/](https://book.sciml.ai/notes/02-Optimizing_Serial_Code/)

[![](https://global.discourse-cdn.com/julialang/original/3X/8/5/85abca5cf022414b3c0f7a8f98fd8e548010cbd2.jpeg "Optimizing Serial Code in Julia 1: Memory Models, Mutation, and Vectorization") ](https://www.youtube.com/watch?v=M2i7sSRcSIw)

[![](https://global.discourse-cdn.com/julialang/original/3X/0/9/0947182566549a263febea8b3de0890dbd59f0e1.jpeg "Code Profiling and Optimization (in Julia)") ](https://www.youtube.com/watch?v=h-xVBD2Pk9o)

Change from Atom to using the VS Code profiler via `@profview`.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [December 23, 2024, 11:42am UTC](https://discourse.julialang.org/t/looking-for-some-best-practices-for-optimizing-julia-code-performance/124117/3 "2024-12-23T11:42:41Z")

</div>

Of course this one: [Performance Tips · The Julia Language](https://docs.julialang.org/en/v1/manual/performance-tips/)

For allocations: [Common allocation mistakes](https://discourse.julialang.org/t/common-allocation-mistakes/66127)

For parallel computing, the docs of this package (and of course the package) are very useful: [OhMyThreads · OhMyThreads.jl](https://juliafolds2.github.io/OhMyThreads.jl/stable/)

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [December 23, 2024, 6:06pm UTC](https://discourse.julialang.org/t/looking-for-some-best-practices-for-optimizing-julia-code-performance/124117/4 "2024-12-23T18:06:12Z")

</div>

There are only a few, very important rules:

- do not use global variables
- put your code into functions
- pre-allocate large arrays and modify them instead of creating new large arrays in a loop
- for small (\<100 elements) arrays use StaticArrays.jl
- when defining structs use concrete types for all elements

OK, some extra rules for threading and strings…

Already `@time` informs you about allocations if called the second time… Try to reduce them to zero, if your functions are simple. Sometimes `@inline` helps.

Always benchmark the most inner functions of your code.

And often a better high-level algorithm has the largest advantage: Using a better solver from DifferentialEquations.jl, or using ModelingToolkit.jl and use its feature to simplify the system of equations symbolically.

My two cents.

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [December 23, 2024, 9:53pm UTC](https://discourse.julialang.org/t/looking-for-some-best-practices-for-optimizing-julia-code-performance/124117/5 "2024-12-23T21:53:03Z")

</div>

[https://modernjuliaworkflows.org/optimizing/](https://modernjuliaworkflows.org/optimizing/)

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [December 23, 2024, 10:14pm UTC](https://discourse.julialang.org/t/looking-for-some-best-practices-for-optimizing-julia-code-performance/124117/6 "2024-12-23T22:14:46Z")

</div>

> [@ufechner7](#):
>
> do not use global variables

I believe you meant untyped or non-const global variables, because there’s no performance penalty for constant globals, or typed ones.
