# Reduce vs. foldl: performance and precision

**URL:** https://discourse.julialang.org/t/reduce-vs-foldl-performance-and-precision/43231
**Category:** Performance
**Tags:** question, unrolling
**Created:** [July 17, 2020, 12:07pm UTC](https://discourse.julialang.org/t/reduce-vs-foldl-performance-and-precision/43231 "2020-07-17T12:07:45Z")
**Posts on this page:** 1
**Showing post:** 7

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [July 17, 2020, 5:16pm UTC](https://discourse.julialang.org/t/reduce-vs-foldl-performance-and-precision/43231/7 "2020-07-17T17:16:49Z")

</div>

> [@StefanKarpinski](#):
>
> strict left-to-right summation cannot use SIMD because that requires reassociating the sum

Oh, right. I was thinking of

```julia
function naivesum(a)
    s = zero(eltype(a))
    @simd for x in a
        s += x
    end
    return s
end

```

which is mostly left-to-right summation but is not strictly left-to-right at a fine-grained level because `@simd` allows some reassociation. This achieves basically the same performance as `sum`:

```julia
julia> @btime sum($a);
  5.047 ms (0 allocations: 0 bytes)

julia> @btime naivesum($a);
  5.166 ms (0 allocations: 0 bytes)

```

---

_[View the full topic](https://discourse.julialang.org/t/reduce-vs-foldl-performance-and-precision/43231)._
