]> granicus.if.org Git - python/commitdiff
bpo-37158: Simplify and speed-up statistics.fmean() (GH-13832) (GH-13843)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 5 Jun 2019 15:18:13 +0000 (08:18 -0700)
committerRaymond Hettinger <rhettinger@users.noreply.github.com>
Wed, 5 Jun 2019 15:18:13 +0000 (08:18 -0700)
(cherry picked from commit 6c01ebcc0dfc6be22950fabb46bdc10dcb6202c9)

Co-authored-by: Raymond Hettinger <rhettinger@users.noreply.github.com>
Lib/statistics.py
Misc/NEWS.d/next/Library/2019-06-04-22-25-38.bpo-37158.JKm15S.rst [new file with mode: 0644]

index 012845b8d2ef4c34524f6d83ce7f27f934f2f9bb..5be70e5ebf4ebb5dfe1f17598a94c76c720c9bf1 100644 (file)
@@ -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 (file)
index 0000000..4a5ec41
--- /dev/null
@@ -0,0 +1 @@
+Speed-up statistics.fmean() by switching from a function to a generator.