https://github.com/python/cpython/commit/55a09ed4227eecbc4a3880637c2250bfc05858ef
commit: 55a09ed4227eecbc4a3880637c2250bfc05858ef
branch: main
author: Raymond Hettinger <[email protected]>
committer: rhettinger <[email protected]>
date: 2026-06-29T14:12:09-05:00
summary:
Micro-optimizations for the statistics module (#152618)
files:
M Lib/statistics.py
diff --git a/Lib/statistics.py b/Lib/statistics.py
index 01ca6c51dafcaf..758b5b58848fb9 100644
--- a/Lib/statistics.py
+++ b/Lib/statistics.py
@@ -894,7 +894,7 @@ def _quartic_invcdf_estimate(p):
@register('quartic', 'biweight')
def quartic_kernel():
- pdf = lambda t: 15/16 * (1.0 - t * t) ** 2
+ pdf = lambda t: 15/16 * (u := 1.0 - t * t) * u
cdf = lambda t: sumprod((3/16, -5/8, 15/16, 1/2),
(t**5, t**3, t, 1.0))
invcdf = _newton_raphson(_quartic_invcdf_estimate, f=cdf, f_prime=pdf)
@@ -1486,15 +1486,13 @@ def _sum(data):
"""
count = 0
types = set()
- types_add = types.add
partials = {}
- partials_get = partials.get
for typ, values in groupby(data, type):
- types_add(typ)
+ types.add(typ)
for n, d in map(_exact_ratio, values):
count += 1
- partials[d] = partials_get(d, 0) + n
+ partials[d] = partials.get(d, 0) + n
if None in partials:
# The sum will be a NAN or INF. We can ignore all the finite
@@ -1524,12 +1522,11 @@ def _ss(data, c=None):
count = 0
types = set()
- types_add = types.add
sx_partials = defaultdict(int)
sxx_partials = defaultdict(int)
for typ, values in groupby(data, type):
- types_add(typ)
+ types.add(typ)
for n, d in map(_exact_ratio, values):
count += 1
sx_partials[d] += n
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]