JU

Julia Performance

Julia performance optimization: type stability, memory allocation, and benchmarking

Details

Language / Topic
juliaJulia
Category
Performance

Rules

balanced
- Use `@benchmark` from `BenchmarkTools.jl` instead of `@time` for accurate micro-benchmarks — `@time` includes JIT compilation on the first call.
- Write type-stable functions — use `@code_warntype f(args...)` to confirm the compiler infers concrete types for all local variables.
- Avoid global variables in hot paths — annotate with `const` or pass as function arguments to prevent type-instability.
- Use `StaticArrays.jl` (`SVector`, `SMatrix`) for small fixed-size arrays — they are stack-allocated and avoid heap pressure.
- Pre-allocate output arrays and use in-place functions (suffixed `!`) in loops instead of creating new arrays on every iteration.
- Use `@inbounds` only after verifying bounds manually — combine with `@simd` on inner loops where vectorization is safe.
- Profile with `Profile.@profile` and visualize with `ProfileView.jl` or `PProf.jl` before optimizing — never guess bottlenecks.