Fastest way to check for Inf or NaN in an array?

Here is the absolute fastest version. It uses a combination of early exits and vectorization to get maximum performance.

function y(x)
    for i in firstindex(x):64:(lastindex(x)-63)
        s = zero(eltype(x))
        @turbo for j in 0:63
            s += x[i+j]*0
        end
        !isfinite(s) && return false
        end
    return all(isfinite, @view x[max(end-64,begin):end])
end

(for sparse arrays, you want to do the same thing, but on nonzeros(x) instead of x)

7 Likes