Optimisation for Julias
Julia optimisation is the discipline of making code fast by helping the compiler: type stability, avoiding global variables, preallocating arrays, reducing allocations and measuring with profilers. It belongs to the Performance stage and explains why the same algorithm can differ by a factor of one hundred depending on how it is written.
Stage: Performance · Part of the Julia roadmap · Last reviewed 19 November 2026
What to learn in Optimisation
- Benchmark a function reliably using BenchmarkTools and the @btime macro
- Detect type instability with @code_warntype and fix it
- Move hot code into functions instead of using untyped global variables
- Reduce allocations by preallocating output arrays and using in-place functions
- Find the slow lines of a program with the Profile standard library
Optimisation interview questions
What is type stability in Julia?
A function is type stable when the type of its return value can be inferred from the types of its inputs. Stable code lets the compiler generate specialised, fast machine code. Unstable code forces runtime checks and allocation, and shows up as Any or Union in @code_warntype.
Why are global variables slow in Julia?
A non-constant global can change type at any time, so the compiler cannot assume anything about it inside a function. Passing the value as an argument or declaring the global const lets the compiler specialise code and removes the overhead.
Where Optimisation fits in the Julia roadmap
Optimisation belongs to the Performance stage (intermediate level). Make it fast.
Other units in this stage
Back to the full Julia roadmap