Skip to content

Commit 714b860

Browse files
authored
SIMD mean() (#43)
1 parent 00e61ba commit 714b860

File tree

1 file changed

+5
-11
lines changed

1 file changed

+5
-11
lines changed

src/NaNMath.jl

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -214,18 +214,12 @@ Returns a tuple of the arithmetic mean of all elements in the array, ignoring Na
214214
and the number of non-NaN values in the array.
215215
"""
216216
function mean_count(x::AbstractArray{T}) where T<:AbstractFloat
217-
sum = convert(eltype(x), NaN)
217+
z = zero(eltype(x))
218+
sum = z
218219
count = 0
219-
for i in x
220-
if !isnan(i)
221-
if isnan(sum)
222-
sum = i
223-
count = 1
224-
else
225-
sum += i
226-
count += 1
227-
end
228-
end
220+
@simd for i in x
221+
count += ifelse(isnan(i), 0, 1)
222+
sum += ifelse(isnan(i), z, i)
229223
end
230224
result = sum / count
231225
return (result, count)

0 commit comments

Comments
 (0)