Skip to content
Tutorials Center
My learning

Topics

Lesson 21 of 40 ยท Performance

Performance Tips

Make Julia code fast: keep functions type stable, avoid global variables, preallocate, use views and in-place functions, loop in column-major order and benchmark properly.

Julia can be as fast as C, but it will not do it for you automatically if the code gives the compiler too little to work with. The good news is that the rules are few, they are mostly about telling the compiler what it needs to know, and you can check each of them with a tool. Fast Julia is usually not clever Julia; it is plain code that avoids a handful of well-known traps.

This lesson walks through the most valuable habits: measure first, keep functions type stable, avoid non-constant globals, preallocate and reuse arrays, use views and in-place operations, loop in the order memory is laid out, and apply @inbounds carefully. The Julia code is read-only on this page. Python with NumPy stands in to show the ideas that carry over, such as the cost of interpreted loops, allocation and memory layout, using real runnable output.

Measure before you optimize

Guessing which line is slow is how time gets wasted. Use @time for a quick look, and the BenchmarkTools package for trustworthy numbers:

JULIA
using BenchmarkTools

v = rand(10_000)

@btime sum($v)        # 2.9 ฮผs (0 allocations: 0 bytes)
@benchmark sum($v)    # full statistics and a histogram

Two details matter. First, @btime runs the code many times and reports the minimum, so one-off compile time is excluded (see How Julia Compiles Code). Second, the $ in $v interpolates the variable's value into the benchmark, so the timing does not include the slow lookup of a global variable. Without it you would measure the overhead of the benchmark setup, not sum. The times above are only illustrative.

Watch the allocation count too. Memory allocated in a hot loop is a common reason for slow code, and @time and @btime report it for free.

The big five habits

Measure @btime, not guesswork.
Type stable Same input types, same output type.
No globals Pass arguments or use const.
Reuse memory Preallocate and work in place.

Check each one with @code_warntype and allocation counts.

1 / 2

Put hot code in functions, and avoid globals

The compiler can only optimize what it can see types for. A variable at the top level of a script that is not declared const could be rebound to a value of any type at any time, so code that reads it cannot be specialized.

JULIA
total = 0.0                   # a non-constant global

function slow_sum(v)
    for x in v
        global total += x     # every access checks the type of total
    end
end

function fast_sum(v)
    total = 0.0               # a local: its type is known
    for x in v
        total += x
    end
    return total
end

fast_sum is typically tens of times faster and allocates nothing, while slow_sum allocates on every iteration because the float must be boxed to be stored in a variable of unknown type. The fixes, in order of preference:

  1. Make the value a function argument or return value (as in fast_sum).
  2. If a global really is a constant, declare it: const LIMIT = 1_000.
  3. Wrap a global in a typed container, or annotate it with x::Float64 = 0.0 (Julia 1.8+), though 1 and 2 are cleaner.

The rule of thumb: write the inner computation as a function and call it from the top level. This is also what Modules and Scope recommended. Code typed straight into the REPL runs at global scope, so benchmarks of loops in the REPL can be misleading.

Keep functions type stable

A function is type stable if its return type depends only on its argument types, not their values. When it does not, the compiler must carry unions or Any, as shown in the previous lesson. Typical sources of instability:

JULIA
# Unstable: result is Int or Float64 depending on the value
f(x) = x > 0 ? x : 0.0

# Stable: zero(x) has the same type as x
g(x) = x > 0 ? x : zero(x)

# Unstable accumulator: starts as Int, becomes Float64 inside the loop
function bad_mean(v)
    s = 0                     # Int64
    for x in v
        s += x                # becomes Float64 for float data
    end
    return s / length(v)
end

# Stable: start with a zero of the right type
function good_mean(v)
    s = zero(eltype(v))
    for x in v
        s += x
    end
    return s / length(v)
end

Other things to avoid: containers of abstract types (Vector{Any}, Vector{Real}), and struct fields declared as abstract types (value::Real makes every access dynamic; use a type parameter, as in Parametric Types and Generic Code). Check with @code_warntype: anything printed as Any or a large Union inside the loop body is a problem.

A useful pattern is the function barrier: if you must read data of unknown type (say from a file), do that in one function, then pass the result to a separate function that does the heavy computation. The second function is compiled once the concrete type is known, and runs at full speed.

Allocate less: preallocation, views and in-place functions

Every new array costs time for allocation and later garbage collection. Reduce it:

JULIA
# Allocates a new vector on every iteration
function slow(n)
    out = Float64[]
    for i in 1:n
        push!(out, sqrt(i))
    end
    return out
end

# Allocates once
function faster(n)
    out = Vector{Float64}(undef, n)    # or zeros(n)
    for i in 1:n
        out[i] = sqrt(i)
    end
    return out
end

# Allocation-free in a hot loop: write into a result that already exists
function sqrt_into!(out, v)
    @. out = sqrt(v)                   # fused, writes in place
    return out
end

Related tools:

  • Functions ending in ! modify an argument (sort!, push!, mul! for matrix products, fill!, copyto!). They avoid allocating a result.
  • @. and dotted calls fuse a chain such as y .= a .* x .+ b into one loop with no temporaries; see Broadcasting and the Dot Syntax.
  • Slicing like A[:, 1] creates a copy. @view A[:, 1] or @views over an expression makes a light wrapper around the original data:
JULIA
col_sum(A) = sum(@view A[:, 1])
@views total = sum(A[2:end, :])

Views are not always faster: for small slices the copy is cheap and a view can be slower to use, so measure.

  • Use similar(x) to get an uninitialized array shaped like x, and StaticArrays.jl for tiny fixed-size vectors that live on the stack.

The same ideas, seen in NumPy

NumPy suffers the same costs. The stand-in below compares a Python loop with one vectorized call, and an allocating expression with an in-place one. We print results that do not depend on machine speed: the equal answers, the object identity, and whether memory is shared.

Python Loops, allocation and views in NumPy

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

In Julia the loop version is not slow: a plain for loop in a type-stable function compiles to machine code just like the vectorized call. That is a key difference from NumPy, where you must avoid loops. The Julia advice about allocation, though, applies equally.

Loop in memory order

Julia arrays are stored column-major: the elements of the first column are next to each other in memory, then the second column, and so on. The CPU loads memory in chunks, so touching neighbours in order is much faster than jumping around.

JULIA
function sum_cols(A)               # inner loop over the first index: fast
    s = 0.0
    for j in axes(A, 2), i in axes(A, 1)
        s += A[i, j]
    end
    return s
end

function sum_rows(A)               # inner loop over the second index: slower
    s = 0.0
    for i in axes(A, 1), j in axes(A, 2)
        s += A[i, j]
    end
    return s
end

In for j in ..., i in ... the variable listed last (i) is the innermost loop, so it changes fastest. For big matrices the cache-friendly version can be several times faster. NumPy defaults to the opposite, row-major layout, so the same habit reverses there. This stand-in prints the strides, the number of bytes between neighbours:

Python Memory layout and strides

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

In the column-major array, one step down a column is 8 bytes (one Float64), so walking down columns is a straight sweep through memory, which is exactly Julia's layout.

Bounds checks, SIMD and other fine-tuning

Once the basics are right, a few annotations squeeze out more:

JULIA
function dot_product(a, b)
    s = zero(eltype(a))
    @inbounds @simd for i in eachindex(a, b)
        s += a[i] * b[i]
    end
    return s
end
  • @inbounds removes the check that each index is valid. If the index is wrong, you get silent memory corruption instead of a clean BoundsError, so use it only in code whose indices are proven safe, for example when looping over eachindex(a), and only after measuring a benefit.
  • @simd allows the compiler to reorder floating-point additions so it can use vector instructions. That slightly changes rounding, so it is opt-in.
  • eachindex(a, b) is safer than 1:length(a): it works with any array type and checks that the shapes agree.
  • Start Julia with -O3 or --check-bounds=no only if you know why. The defaults are good.
  • Multithreading and GPUs come later, in Threads, Tasks and Distributed Computing.

Do not apply @inbounds everywhere "for speed". It removes a safety net and often changes little. Fix type stability and allocations first, which usually give 10x to 100x, before reaching for tweaks that give 10 percent.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why does @btime sum($v) use $v instead of v?

    Without $, the benchmark reads v as a global variable, and the lookup overhead pollutes the measurement.
  2. 2. Which change most likely fixes a slow loop that updates a top-level variable total?

    A non-constant global has an unknown type, so the compiler cannot specialize. A local variable inside a function has a known type.
  3. 3. In column-major order, which nested loop is the cache-friendly one for A[i, j]?

    The first index varies fastest in memory, so it should change in the innermost loop.
  4. 4. What does @view A[:, 1] do differently from A[:, 1]?

    A view shares memory with the parent array, so writes through the view change the original and no new array is allocated.
  5. 5. Which declaration helps make a struct field access type stable?

    An abstract field type forces dynamic lookups on every access. A type parameter lets the compiler know the concrete type of each instance.

Summary

  • Measure with @btime and interpolate arguments with $; watch allocation counts too.
  • Put hot code in functions, avoid non-constant globals, and keep functions type stable; check with @code_warntype.
  • Preallocate, use ! functions, fused dot calls and @views to avoid creating temporary arrays.
  • Julia arrays are column-major, so loop with the first index innermost.
  • @inbounds and @simd are last-mile tweaks; apply them only after the basics and only where safe.

Next, spread the work over several cores in Threads, Tasks and Distributed Computing.