# Performance of streaming \`mean\`

**URL:** <https://discourse.julialang.org/t/performance-of-streaming-mean/106999>\
**Category:** Performance\
**Created:** [December 1, 2023, 3:41pm UTC](https://discourse.julialang.org/t/performance-of-streaming-mean/106999 "2023-12-01T15:41:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [December 1, 2023, 3:41pm UTC](https://discourse.julialang.org/t/performance-of-streaming-mean/106999/1 "2023-12-01T15:41:17Z")

</div>

Here’s a basic streaming `mean` function (called `avg` for convenience):

```julia
function avg(itr)
    a = zero(eltype(x))
    for (i, x) in enumerate(itr)
        a += (x - a) / i
    end
    return a
end

```

Why does it allocate so much on a `skipmissing` iterator? Is there some issue with the `skipmissing` iterator implementation?

```julia
julia> x = rand(Float32, 100_000_000); sx = skipmissing(x);

julia> @btime avg($sx);
  10.356 s (499999489 allocations: 7.45 GiB)

```

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [December 1, 2023, 3:46pm UTC](https://discourse.julialang.org/t/performance-of-streaming-mean/106999/2 "2023-12-01T15:46:37Z")

</div>

Huh, this also has a ton of allocations:

```julia
julia> @btime avg(1:1_000_000);
  69.484 ms (4998978 allocations: 76.28 MiB)

```

Am I missing something obvious? 😂

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [December 1, 2023, 3:48pm UTC](https://discourse.julialang.org/t/performance-of-streaming-mean/106999/3 "2023-12-01T15:48:24Z")

</div>

Oops, dumb mistake… This is what happens when I work in the REPL and try to edit functions.

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [December 1, 2023, 3:48pm UTC](https://discourse.julialang.org/t/performance-of-streaming-mean/106999/4 "2023-12-01T15:48:37Z")

</div>

It should be `eltype(itr)`? I’m not sure why it works at all 🤣

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [December 1, 2023, 3:49pm UTC](https://discourse.julialang.org/t/performance-of-streaming-mean/106999/5 "2023-12-01T15:49:25Z")

</div>

Yeah, I should have been working in VS Code. I make stupid editing mistakes when I edit functions in the REPL.
