Lesson 3 of 9 · 28m
Type Stability, Specialization, and Honest Benchmarking
Use Julia's compiler model, BenchmarkTools, and allocation profiles to separate real performance problems from measurement artifacts.
The fastest way to become disappointed in Julia is to write Python-shaped code in global scope, time the first run, and declare the language slow. Julia's speed comes from specialization: functions compile for concrete argument types. Your job as a practitioner is to keep types inferrable through hot code and to measure after compilation instead of mistaking compile time for runtime.
Put hot code in functions
At top level, accumulator variables are globals whose types can change. Inside a function, the compiler can often prove exact types:
function total(x)
s = zero(eltype(x))
for v in x
s += v
end
return s
endzero(eltype(x)) starts with the right numeric type instead of forcing an integer zero to promote during the loop.
Type instability means the compiler lost the thread
A function is type-unstable when the return type depends on runtime values in a way inference cannot predict:
bad(flag) = flag ? 1.0 : "one"Now callers cannot know whether arithmetic is legal. Type instability does not always make code wrong, but in inner loops it often forces dynamic dispatch and allocation.
Use @inferred total(randn(1000)) in tests for functions that must stay stable. @code_warntype bad(true) highlights types inference cannot settle. Read it as a diagnostic, not as a puzzle to make every expression concrete; some dynamic behavior is legitimate.
Benchmark correctly
Never benchmark first-call compilation unless startup latency is what you are studying. BenchmarkTools runs enough samples to reduce noise:
using BenchmarkTools
x = randn(100_000)
@btime total($x)
@benchmark total($x)The $ interpolates x as a fixed input rather than benchmarking global lookup. A suspicious result might report 123.456 μs (2 allocations: 781.33 KiB). Unexpected allocations often matter as much as time because they trigger garbage collection under load.
Do not chase zero allocations blindly
A temporary array can be cheaper than a cache-hostile in-place algorithm. Measure the real operation sequence. Scientific workloads often care about predictable memory bandwidth, parallel scaling, and numerical reproducibility as much as one microbenchmark. Treat @inbounds and @simd as promises to the compiler: use them only after profiling proves both the benefit and the safety case.
Field notes
- Time at least twice: first call includes compilation; subsequent calls measure the specialized machine code.
- Functions that return different concrete types based on input values can poison every downstream calculation.
AbstractVectorinput is fine; unstable element types inside the vector are the real problem.- Keep benchmark inputs outside timed closures and use
$xin BenchmarkTools expressions. - A small microbenchmark win can be irrelevant if compilation latency dominates an interactive workflow.
Was this lesson helpful?