You’re allocating an array of indices here, then doing an operation for each index, then throwing away that array of indices. Instead, you could avoid the allocation entirely by adding another loop.
For example, let’s say we wanted to check which numbers in 1:N are even (not a hard problem, but an easy example). Your code is doing something akin to:
function find_evens_1(N)
x = zeros(Bool, N)
# Allocate a vector of indices
inds = findall(iseven, 1:N)
# Use those indices all at once
x[inds] .= true
end
I am suggesting, instead, that you do:
function find_evens_2(N)
x = zeros(Bool, N)
for i in 1:N
if iseven(i)
x[i] = true
end
end
end
Let’s compare performance:
julia> @btime find_evens_1($(1000));
4.492 μs (14 allocations: 9.55 KiB)
julia> @btime find_evens_2($(1000));
697.279 ns (1 allocation: 1.06 KiB)
The explicit loop is more than 6 times faster than the “vectorized” use of findall and allocates 9 times less memory.