Skip to content

Statistics internals: Make fewer calls to _coerce() when data types are mixed #31619

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 40 commits into from
Feb 28, 2022
Merged
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
bbd2da9
Merge pull request #1 from python/master
rhettinger Mar 16, 2021
74bdf1b
Merge branch 'master' of github.com:python/cpython
rhettinger Mar 22, 2021
6c53f1a
Merge branch 'master' of github.com:python/cpython
rhettinger Mar 22, 2021
a487c4f
.
rhettinger Mar 24, 2021
eb56423
.
rhettinger Mar 25, 2021
cc7ba06
.
rhettinger Mar 26, 2021
d024dd0
.
rhettinger Apr 22, 2021
b10f912
merge
rhettinger May 5, 2021
fb6744d
merge
rhettinger May 6, 2021
7f21a1c
Merge branch 'main' of github.com:python/cpython
rhettinger Aug 15, 2021
7da42d4
Merge branch 'main' of github.com:rhettinger/cpython
rhettinger Aug 25, 2021
e31757b
Merge branch 'main' of github.com:python/cpython
rhettinger Aug 31, 2021
f058a6f
Merge branch 'main' of github.com:python/cpython
rhettinger Aug 31, 2021
1fc29bd
Merge branch 'main' of github.com:python/cpython
rhettinger Sep 4, 2021
e5c0184
Merge branch 'main' of github.com:python/cpython
rhettinger Oct 30, 2021
3c86ec1
Merge branch 'main' of github.com:python/cpython
rhettinger Nov 9, 2021
96675e4
Merge branch 'main' of github.com:rhettinger/cpython
rhettinger Nov 9, 2021
de558c6
Merge branch 'main' of github.com:python/cpython
rhettinger Nov 9, 2021
61bca51
Merge branch 'main' of github.com:python/cpython
rhettinger Nov 14, 2021
418a07f
Merge branch 'main' of github.com:python/cpython
rhettinger Nov 14, 2021
ea23a8b
Merge branch 'main' of github.com:python/cpython
rhettinger Nov 21, 2021
ba248b7
Merge branch 'main' of github.com:python/cpython
rhettinger Nov 27, 2021
6a3b1ca
Merge branch 'main' of github.com:python/cpython
rhettinger Nov 28, 2021
9bc1df1
Merge branch 'main' of github.com:python/cpython
rhettinger Dec 1, 2021
d4466ba
Merge branch 'main' of github.com:python/cpython
rhettinger Dec 1, 2021
014a352
Merge branch 'main' of github.com:python/cpython
rhettinger Dec 5, 2021
a89f02e
Merge branch 'main' of github.com:python/cpython
rhettinger Dec 8, 2021
aae9a5f
Merge branch 'main' of github.com:python/cpython
rhettinger Dec 10, 2021
761d99e
Merge branch 'main' of github.com:rhettinger/cpython
rhettinger Dec 17, 2021
da9bbe5
Merge branch 'main' of github.com:python/cpython
rhettinger Dec 31, 2021
7ba634b
Merge branch 'main' of github.com:python/cpython
rhettinger Jan 1, 2022
1dba221
Merge branch 'main' of github.com:python/cpython
rhettinger Jan 4, 2022
e1a7b27
Merge branch 'main' of github.com:python/cpython
rhettinger Jan 5, 2022
56e41a3
Merge branch 'main' of github.com:rhettinger/cpython
rhettinger Jan 8, 2022
9736650
Merge branch 'main' of github.com:python/cpython
rhettinger Jan 27, 2022
3120a34
Merge branch 'main' of github.com:python/cpython
rhettinger Jan 28, 2022
600be63
Merge branch 'main' of github.com:python/cpython
rhettinger Feb 6, 2022
11ccb4e
Merge branch 'main' of github.com:python/cpython
rhettinger Feb 27, 2022
50ca068
Make fewer calls to _coerce() when data types are mixed
rhettinger Feb 28, 2022
7e298c5
Make types.add into a boundmethod
rhettinger Feb 28, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions Lib/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@
from itertools import groupby, repeat
from bisect import bisect_left, bisect_right
from math import hypot, sqrt, fabs, exp, erf, tau, log, fsum
from functools import reduce
from operator import mul
from collections import Counter, namedtuple, defaultdict

Expand Down Expand Up @@ -183,11 +184,12 @@ def _sum(data):
allowed.
"""
count = 0
types = set()
types_add = types.add
partials = {}
partials_get = partials.get
T = int
for typ, values in groupby(data, type):
T = _coerce(T, typ) # or raise TypeError
types_add(typ)
for n, d in map(_exact_ratio, values):
count += 1
partials[d] = partials_get(d, 0) + n
Expand All @@ -199,6 +201,7 @@ def _sum(data):
else:
# Sum all the partial sums using builtin sum.
total = sum(Fraction(n, d) for d, n in partials.items())
T = reduce(_coerce, types, int) # or raise TypeError
return (T, total, count)


Expand All @@ -214,11 +217,12 @@ def _ss(data, c=None):
T, total, count = _sum((d := x - c) * d for x in data)
return (T, total, count)
count = 0
types = set()
types_add = types.add
sx_partials = defaultdict(int)
sxx_partials = defaultdict(int)
T = int
for typ, values in groupby(data, type):
T = _coerce(T, typ) # or raise TypeError
types_add(typ)
for n, d in map(_exact_ratio, values):
count += 1
sx_partials[d] += n
Expand All @@ -236,6 +240,7 @@ def _ss(data, c=None):
# This formula has poor numeric properties for floats,
# but with fractions it is exact.
total = (count * sxx - sx * sx) / count
T = reduce(_coerce, types, int) # or raise TypeError
return (T, total, count)


Expand Down