Skip to content

Instantly share code, notes, and snippets.

@SteadBytes
Last active November 30, 2019 12:21
Show Gist options
  • Save SteadBytes/ae934f51eb27229d5412a65f14ce344a to your computer and use it in GitHub Desktop.
Save SteadBytes/ae934f51eb27229d5412a65f14ce344a to your computer and use it in GitHub Desktop.
from itertools import count
from functools import reduce
def iter_mean(iterable):
"""
Caluculate the arithmetic mean of an iterable *without* loading the entire collection
into memory or knowing the legnth apriori.
"""
return reduce(
lambda c, i: (c * (i[1] - 1) + i[0]) / i[1], zip(iterable, count(1)), 0
)
@SteadBytes
Copy link
Author

SteadBytes commented Nov 30, 2019

IPython %timeit examples:

In [1]: from statistics import mean

In [2]: %timeit mean(list(range(10 **6)))                                                                      
355 ms ± 11.4 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [3]: %timeit iter_mean(range(10**6))
153 ms ± 371 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment