summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRaymond Hettinger <rhettinger@users.noreply.github.com>2019-06-05 07:39:38 -0700
committerGitHub <noreply@github.com>2019-06-05 07:39:38 -0700
commit6c01ebcc0dfc6be22950fabb46bdc10dcb6202c9 (patch)
treed3bd04348be7136bd34284c8d1e3616b9679f614
parentccf0efbb21f6bbf6efd5f8cb560fed11079ce1a2 (diff)
downloadcpython-git-6c01ebcc0dfc6be22950fabb46bdc10dcb6202c9.tar.gz
bpo-37158: Simplify and speed-up statistics.fmean() (GH-13832)
-rw-r--r--Lib/statistics.py8
-rw-r--r--Misc/NEWS.d/next/Library/2019-06-04-22-25-38.bpo-37158.JKm15S.rst1
2 files changed, 5 insertions, 4 deletions
diff --git a/Lib/statistics.py b/Lib/statistics.py
index 012845b8d2..5be70e5ebf 100644
--- a/Lib/statistics.py
+++ b/Lib/statistics.py
@@ -320,11 +320,11 @@ def fmean(data):
except TypeError:
# Handle iterators that do not define __len__().
n = 0
- def count(x):
+ def count(iterable):
nonlocal n
- n += 1
- return x
- total = fsum(map(count, data))
+ for n, x in enumerate(iterable, start=1):
+ yield x
+ total = fsum(count(data))
else:
total = fsum(data)
try:
diff --git a/Misc/NEWS.d/next/Library/2019-06-04-22-25-38.bpo-37158.JKm15S.rst b/Misc/NEWS.d/next/Library/2019-06-04-22-25-38.bpo-37158.JKm15S.rst
new file mode 100644
index 0000000000..4a5ec4122f
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2019-06-04-22-25-38.bpo-37158.JKm15S.rst
@@ -0,0 +1 @@
+Speed-up statistics.fmean() by switching from a function to a generator.