Chapter 9. Performance

using Pkg
using BenchmarkTools

iterations = zeros(10000)
println("")

1. Faster for-loop using @inbounds

iterations = zeros(1000000)


function iter_with_inbounds(arr)
    @inbounds for i in 1:length(arr)
        arr[i] = 1.0
    end
end

function iter(arr)
    for i in 1:length(arr)
        arr[i] = 1.0
    end
end

Time without inbounds:

Time with inbounds:

2. Avoid untyped global variables

If a global variable is needed, annotate the type in a loop:

3. Check performance

4. More on...

See: https://docs.julialang.org/en/v1/manual/performance-tips/

Last updated

Was this helpful?