From e35ba2a74007fba5f1e2fb8c741cb2ed57383f6e Mon Sep 17 00:00:00 2001 From: Yu-Sheng Li Date: Sun, 29 Oct 2023 15:42:08 +0900 Subject: [PATCH 1/5] gh-102221: speed up math.lcm by swapping numbers --- .../2023-10-29-15-35-57.gh-issue-102221.fQnOaT.rst | 1 + Modules/mathmodule.c | 12 ++++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2023-10-29-15-35-57.gh-issue-102221.fQnOaT.rst diff --git a/Misc/NEWS.d/next/Library/2023-10-29-15-35-57.gh-issue-102221.fQnOaT.rst b/Misc/NEWS.d/next/Library/2023-10-29-15-35-57.gh-issue-102221.fQnOaT.rst new file mode 100644 index 00000000000000..7ab6b52cdf037a --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-10-29-15-35-57.gh-issue-102221.fQnOaT.rst @@ -0,0 +1 @@ +Speed up math lcm diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index bbbb49115681de..c9c1bf7221a2c4 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -769,7 +769,15 @@ long_lcm(PyObject *a, PyObject *b) if (_PyLong_IsZero((PyLongObject *)a) || _PyLong_IsZero((PyLongObject *)b)) { return PyLong_FromLong(0); } - g = _PyLong_GCD(a, b); + + /* Make sure a <= b to speed up (a // g) * b; see #102221 for details. */ + if (_PyLong_DigitCount((PyLongObject *)b) < _PyLong_DigitCount((PyLongObject *)a)) { + g = a; + a = b; + b = g; + } + + g = _PyLong_GCD(b, a); if (g == NULL) { return NULL; } @@ -830,7 +838,7 @@ math_lcm_impl(PyObject *module, PyObject * const *args, Py_DECREF(x); continue; } - Py_SETREF(res, long_lcm(res, x)); + Py_SETREF(res, long_lcm(x, res)); Py_DECREF(x); if (res == NULL) { return NULL; From 7a02a58aa7ad20bd4f0080966f066e58aa1b006c Mon Sep 17 00:00:00 2001 From: Yu-Sheng Li Date: Sun, 15 Jun 2025 16:13:30 +0800 Subject: [PATCH 2/5] Update doc change Co-authored-by: Sergey B Kirpichev --- .../next/Library/2023-10-29-15-35-57.gh-issue-102221.fQnOaT.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2023-10-29-15-35-57.gh-issue-102221.fQnOaT.rst b/Misc/NEWS.d/next/Library/2023-10-29-15-35-57.gh-issue-102221.fQnOaT.rst index 7ab6b52cdf037a..f2dae7f4e269b0 100644 --- a/Misc/NEWS.d/next/Library/2023-10-29-15-35-57.gh-issue-102221.fQnOaT.rst +++ b/Misc/NEWS.d/next/Library/2023-10-29-15-35-57.gh-issue-102221.fQnOaT.rst @@ -1 +1 @@ -Speed up math lcm +Optimize :func:`math.lcm` by swapping arguments (smaller by absolute value --- first). From 02c4f1726185287b08ee210174f7e7bc61607b53 Mon Sep 17 00:00:00 2001 From: Yu-Sheng Li Date: Sun, 15 Jun 2025 16:14:44 +0800 Subject: [PATCH 3/5] Update comment of abs value and pr Co-authored-by: Sergey B Kirpichev --- Modules/mathmodule.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index c9c1bf7221a2c4..dce777d66fe972 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -770,7 +770,7 @@ long_lcm(PyObject *a, PyObject *b) return PyLong_FromLong(0); } - /* Make sure a <= b to speed up (a // g) * b; see #102221 for details. */ + /* Make sure |a| <= |b| to speed up (a // g) * b; see gh-102221 for details. */ if (_PyLong_DigitCount((PyLongObject *)b) < _PyLong_DigitCount((PyLongObject *)a)) { g = a; a = b; From 88242b1053af8bbfaab73d5c756d3096ac80d58d Mon Sep 17 00:00:00 2001 From: Yu-Sheng Li Date: Wed, 18 Jun 2025 22:53:01 +0800 Subject: [PATCH 4/5] Update Modules/mathmodule.c with more precise comparison Co-authored-by: Sergey B Kirpichev --- Modules/mathmodule.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index dce777d66fe972..529050c62782a9 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -770,7 +770,7 @@ long_lcm(PyObject *a, PyObject *b) return PyLong_FromLong(0); } - /* Make sure |a| <= |b| to speed up (a // g) * b; see gh-102221 for details. */ + /* Make sure a_size <= b_size to speed up (a // g) * b; see gh-102221 for details. */ if (_PyLong_DigitCount((PyLongObject *)b) < _PyLong_DigitCount((PyLongObject *)a)) { g = a; a = b; From c1fa677500131652c6f191cbb30335f6d23c60d6 Mon Sep 17 00:00:00 2001 From: Yu-Sheng Li Date: Thu, 19 Jun 2025 01:21:54 +0800 Subject: [PATCH 5/5] Polish news message --- .../next/Library/2023-10-29-15-35-57.gh-issue-102221.fQnOaT.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2023-10-29-15-35-57.gh-issue-102221.fQnOaT.rst b/Misc/NEWS.d/next/Library/2023-10-29-15-35-57.gh-issue-102221.fQnOaT.rst index f2dae7f4e269b0..136a3e2bc4ca6c 100644 --- a/Misc/NEWS.d/next/Library/2023-10-29-15-35-57.gh-issue-102221.fQnOaT.rst +++ b/Misc/NEWS.d/next/Library/2023-10-29-15-35-57.gh-issue-102221.fQnOaT.rst @@ -1 +1 @@ -Optimize :func:`math.lcm` by swapping arguments (smaller by absolute value --- first). +Speed up :func:`math.lcm` for larger number of arguments.