Skip to content

Commit

Permalink
Improve count performance (JuliaLang#40564)
Browse files Browse the repository at this point in the history
  • Loading branch information
bkamins authored Nov 18, 2021
1 parent 85f4db2 commit ebcce3f
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions base/reduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1221,10 +1221,12 @@ count(itr; init=0) = count(identity, itr; init)

count(f, itr; init=0) = _simple_count(f, itr, init)

function _simple_count(pred, itr, init::T) where {T}
_simple_count(pred, itr, init) = _simple_count_helper(Generator(pred, itr), init)

function _simple_count_helper(g, init::T) where {T}
n::T = init
for x in itr
n += pred(x)::Bool
for x in g
n += x::Bool
end
return n
end
Expand Down

0 comments on commit ebcce3f

Please sign in to comment.