Skip to content

Commit

Permalink
use K-B-N summation for float arrays, with thanks to @JeffreySarnoff
Browse files Browse the repository at this point in the history
written carefully it is no more than 20% slower
closes #199
  • Loading branch information
JeffBezanson committed Jul 6, 2012
1 parent a408bcc commit 19ff52a
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1185,6 +1185,27 @@ function sum{T}(A::StridedArray{T})
v
end

function sum{T<:Float}(A::StridedArray{T})
n = length(A)
if (n == 0)
return zero(T)
end
s = A[1]
c = zero(T)
for i in 2:n
Ai = A[i]
t = s + Ai
if abs(s) >= abs(Ai)
c += ((s-t) + Ai)
else
c += ((Ai-t) + s)
end
s = t
end

s + c
end

function prod{T}(A::StridedArray{T})
if isempty(A)
return one(T)
Expand Down

1 comment on commit 19ff52a

@StefanKarpinski
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's impressive performance. Very nice.

Please sign in to comment.