[Python-checkins] bpo-37158: Simplify and speed-up statistics.fmean() (GH-13832)
Raymond Hettinger
webhook-mailer at python.org
Wed Jun 5 10:40:01 EDT 2019
https://github.com/python/cpython/commit/6c01ebcc0dfc6be22950fabb46bdc10dcb6202c9
commit: 6c01ebcc0dfc6be22950fabb46bdc10dcb6202c9
branch: master
author: Raymond Hettinger <rhettinger at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2019-06-05T07:39:38-07:00
summary:
bpo-37158: Simplify and speed-up statistics.fmean() (GH-13832)
files:
A Misc/NEWS.d/next/Library/2019-06-04-22-25-38.bpo-37158.JKm15S.rst
M Lib/statistics.py
diff --git a/Lib/statistics.py b/Lib/statistics.py
index 012845b8d2ef..5be70e5ebf4e 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 000000000000..4a5ec4122f94
--- /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.
More information about the Python-checkins
mailing list