Sum isn't faster than a loop?

Note that you can also write this:

    v = view(x, 1:length(x)-1)
    ∑μ = sum(fitted, v)

or this

using LazyArrays: @~ # just to construct Base.Broadcast.Broadcasted
...
    v = view(x, 1:length(x)-1)
    ∑μ = sum(@~ fitted.(v))

Both should avoid the allocations, and the lazy broadcast sum is fast on Julia 1.5. They won’t beat the simple loop, but can be tidier.

5 Likes