a = rand(10^7)
function mysum(A)
s = 0.0
for a in A
s += a
end
return s
end
12s
sum(a)
6s
function mysum_fast(A)
s = 0.0
for a in A
@fastmath s += a
end
s
end
6s
The for
loop
for a in A
s += a
end
defines a very strict order to the summation: Julia follows exactly what you
wrote and adds the elements of A
to the result s
in the order it iterates.
Since floating point numbers aren't associative, a rearrangement here would
change the answer — and Julia is loathe to give you different answer than
the one you asked for.
You can, however, tell Julia to relax that rule and allow for associativity
with the @fastmath
macro. This might allow Julia to rearrange the sum in an
advantageous manner.