Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k 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 Reply

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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...