summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2019-06-05 15:18:13 (GMT)
committerRaymond Hettinger <rhettinger@users.noreply.github.com>2019-06-05 15:18:13 (GMT)
commit9ddb77741e041966d64739353393bcba33cc3bdd (patch)
tree971919a48e13353dfb97483b702428c19690e188
parentb496c2672131ea51a55b5a414aeda271562f18d3 (diff)
downloadcpython-9ddb77741e041966d64739353393bcba33cc3bdd.zip
cpython-9ddb77741e041966d64739353393bcba33cc3bdd.tar.gz
cpython-9ddb77741e041966d64739353393bcba33cc3bdd.tar.bz2
bpo-37158: Simplify and speed-up statistics.fmean() (GH-13832) (GH-13843)
(cherry picked from commit 6c01ebcc0dfc6be22950fabb46bdc10dcb6202c9) Co-authored-by: Raymond Hettinger <rhettinger@users.noreply.github.com>
-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 012845b..5be70e5 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 0000000..4a5ec41
--- /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.