Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

julia - 朱莉娅(Julia):如何有效计算Vector {Union {T,Missing}}中的遗失数量(Julia: How to count efficiently the number of missings in a `Vector{Union{T, Missing}}`)

Consider

(考虑)

x = rand([missing, rand(Int, 100)...], 1_000_000)

which yields typeof(x) = Array{Union{Missing, Int64},1} .

(产生typeof(x) = Array{Union{Missing, Int64},1} 。)

What's the most efficient way to count the number of missings in x ?

(计算x的遗失次数最有效的方法是什么?)

  ask by xiaodai translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

(无成本的抽象,只是您想要的方式。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...