-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add init argument to count #37461
add init argument to count #37461
Conversation
for x in itr | ||
n += pred(x)::Bool | ||
end | ||
return n | ||
end | ||
|
||
function count(::typeof(identity), x::Array{Bool}) | ||
n = 0 | ||
function _simple_count(::typeof(identity), x::Array{Bool}, init::T=0) where {T} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is actually a bugfix (although it doesn't occur in 1.5), because count(::Array{Bool})
currently errors when passed a dims
argument.
Interesting idea. Though I wonder whether the speedup obtained by using a smaller integer type isn't just due to a compiler bug in this particular case: see #37473. I don't see any speedup when counting non-missing values or the number of zeros: julia> function count_nonmissing(x, T)
c = zero(T)
for i in 1:length(x)
c += @inbounds x[i] !== missing
end
return Int(c)
end
count_nonmissing (generic function with 1 method)
julia> x = rand([missing, rand(Int, 100)...], 1_000_000);
julia> @btime count_nonmissing(x, Int);
25.500 ms (999485 allocations: 15.25 MiB)
julia> @btime count_nonmissing(x, Int16);
27.453 ms (983978 allocations: 15.01 MiB)
julia> function count_zero(x, T)
c = zero(T)
for i in 1:length(x)
c += @inbounds x[i] == 0
end
return Int(c)
end
julia> x = rand(1_000_000);
julia> @btime count_zero(x, Int);
21.660 ms (0 allocations: 0 bytes)
julia> @btime count_zero(x, Int16);
19.200 ms (0 allocations: 0 bytes) |
Hmm, interesting! I would still argue that this is worth it for the consistency alone. It still seems useful to be able to control the accumulation type, perhaps someone wants to count using finite fields, or explicitely needs 64 bits of accuracy on 32-bit platforms. |
I don't know who would be best to review this. @tkf, would you be willing to take a quick look at this, since I think you worked a lot on our reduce functions lately? |
For now, I guess you can just use TBH, after reading #35947 (comment), I've been wondering how much we should be doing for supporting |
I think currently |
Isn't it better to use these methods from
I feel this is, in principle, minor than assigning different semantics to |
Bump? |
@tkf Are you fine with this as-is, or do you still think, |
3e4b386
to
ec0d730
Compare
Bump. |
@rfourquet Would you mind taking another look at this before the branching for 1.6 happens, because |
Oh sorry, I had missed your previous review request. I will have a look later today. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Just the docstring needs to document init
.
Co-authored-by: Rafael Fourquet <fourquet.rafael@gmail.com>
ec0d730
to
b8d860c
Compare
Thank you very much for the thorough review! Hope I addressed it all. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a couple more nitpicks :)
Co-authored-by: Rafael Fourquet <fourquet.rafael@gmail.com>
Is this good to go? |
I will merge in a couple of days if no objection :) |
There's also a bunch of issue links that are missing (JuliaLang#37410, JuliaLang#37247, JuliaLang#37540, JuliaLang#37973, JuliaLang#37461, JuliaLang#37753) but it seems there's a script that generates the links so I'm assuming that will be fixed automatically. Co-authored-by: Viral B. Shah <ViralBShah@users.noreply.github.com>
Not sure why this worked before JuliaLang#37461, perhaps JuliaLang#9498?
Inspired by this StackOverflow answer. Unfortunately,
count!
also already takes aninit
argument, which has a different meaning, so this will be the same situation as we currently have withsum
/sum!
, see #36266.