-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Closed
Labels
missing dataBase.missing and related functionalityBase.missing and related functionalityperformanceMust go fasterMust go faster
Description
Since #27651 a loop involving x === missing can be very fast, but using the more generic ismissing(x) leads to much less efficient code being generated. Apparently, the compiler isn't able to detect that !ismissing(x) implies that x::Int.
function mysum(X)
s = zero(eltype(X))
@inbounds @simd for x in X
if x !== missing
s += x
end
end
s
end
function mysum2(X)
s = zero(eltype(X))
@inbounds @simd for x in X
if !ismissing(x)
s += x
end
end
s
end
X1 = rand(Int, 10_000_000);
X2 = Vector{Union{Missing,Int}}(X1);
X3 = ifelse.(rand(length(X2)) .< 0.9, X2, missing);
julia> @btime mysum(X1);
5.631 ms (1 allocation: 16 bytes)
julia> @btime mysum(X2);
5.785 ms (1 allocation: 16 bytes)
julia> @btime mysum(X3);
5.670 ms (1 allocation: 16 bytes)
julia> @btime mysum2(X1);
5.676 ms (1 allocation: 16 bytes)
julia> @btime mysum2(X2);
16.377 ms (1 allocation: 16 bytes)
julia> @btime mysum2(X3);
20.681 ms (1 allocation: 16 bytes)Nosferican
Metadata
Metadata
Assignees
Labels
missing dataBase.missing and related functionalityBase.missing and related functionalityperformanceMust go fasterMust go faster