]> granicus.if.org Git - python/commitdiff
bpo-37158: Simplify and speed-up statistics.fmean() (GH-13832)
authorRaymond Hettinger <rhettinger@users.noreply.github.com>
Wed, 5 Jun 2019 14:39:38 +0000 (07:39 -0700)
committerGitHub <noreply@github.com>
Wed, 5 Jun 2019 14:39:38 +0000 (07:39 -0700)
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.