Looking for Some Best Practices for Optimizing Julia Code Performance?

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

1 Like

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/

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

1 Like

Of course this one: Performance Tips · The Julia Language

For allocations: Common allocation mistakes

For parallel computing, the docs of this package (and of course the package) are very useful: OhMyThreads · OhMyThreads.jl

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.

3 Likes

https://modernjuliaworkflows.org/optimizing/

4 Likes

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