The cleanest way is probably just
(最干净的方法可能只是)
count(ismissing, x)
Simple, easy to remember, and fast
(简单,易记且快速)
Since you're asking for the "most efficient" way, let me give some benchmark results.
(由于您正在寻求“最有效”的方式,因此让我给出一些基准测试结果。)
It is slightly faster than @xiaodai's answer, and as fast as a simple loop implementation: (它比@xiaodai的答案稍快,并且与简单的循环实现一样快:)
julia> @btime count($ismissing,$x);
278.499 μs (0 allocations: 0 bytes)
julia> @btime mapreduce($ismissing, $+, $x);
293.901 μs (0 allocations: 0 bytes)
julia> @btime count_missing($x)
278.499 μs (0 allocations: 0 bytes)
where
(哪里)
julia> function count_missing(x)
c = 0
@inbounds for i in eachindex(x)
if ismissing(x[i])
c += 1
end
end
return c
end
Abstraction for no cost, just the way you'd want it to be.
(无成本的抽象,只是您想要的方式。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…