From f0bdcf3e92746fbea407c6bd9d2444de8700e8f2 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Mon, 19 Jan 2026 15:17:23 +0300 Subject: [PATCH 1/6] Enable decimal benchmarks in the suite (MANIFEST) They were not added here in #341 for no reasons. --- pyperformance/data-files/benchmarks/MANIFEST | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pyperformance/data-files/benchmarks/MANIFEST b/pyperformance/data-files/benchmarks/MANIFEST index 8b28b9db..5ca9a187 100644 --- a/pyperformance/data-files/benchmarks/MANIFEST +++ b/pyperformance/data-files/benchmarks/MANIFEST @@ -38,6 +38,8 @@ comprehensions crypto_pyaes dask deepcopy +decimal_factorial +decimal_pi deltablue django_template dulwich_log From 04cae25a4b3b4b3a34a9c99cd2ef05b124d7a446 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Mon, 19 Jan 2026 15:58:50 +0300 Subject: [PATCH 2/6] Update pyperformance/data-files/benchmarks/MANIFEST Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> --- pyperformance/data-files/benchmarks/MANIFEST | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyperformance/data-files/benchmarks/MANIFEST b/pyperformance/data-files/benchmarks/MANIFEST index 5ca9a187..202de64c 100644 --- a/pyperformance/data-files/benchmarks/MANIFEST +++ b/pyperformance/data-files/benchmarks/MANIFEST @@ -37,9 +37,9 @@ chaos comprehensions crypto_pyaes dask -deepcopy decimal_factorial decimal_pi +deepcopy deltablue django_template dulwich_log From 9fb5802a8a331b54df97fd0aafb309fed7ba1667 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Tue, 20 Jan 2026 10:37:17 +0300 Subject: [PATCH 3/6] Apply suggestion from @skirpichev --- pyperformance/data-files/benchmarks/MANIFEST | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pyperformance/data-files/benchmarks/MANIFEST b/pyperformance/data-files/benchmarks/MANIFEST index 202de64c..69aea2b9 100644 --- a/pyperformance/data-files/benchmarks/MANIFEST +++ b/pyperformance/data-files/benchmarks/MANIFEST @@ -37,7 +37,8 @@ chaos comprehensions crypto_pyaes dask -decimal_factorial +# FIXME: this doesn't work with `_pydecimal` +# decimal_factorial decimal_pi deepcopy deltablue From 243a809d68fbf8e58483f63888c630272f17b6c1 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Sun, 22 Feb 2026 03:14:09 +0300 Subject: [PATCH 4/6] Tune bench_decimal_factorial() to support pure-Python decimal module Tested locally with modified Modules/_decimal/tests/bench.py: n = 1000 pydecimal: calculation time: 3.147357s conversion time: 0.000016s int: calculation time: 0.001096s conversion time: 0.000271s n = 5000 pydecimal: calculation time: 15.728110s conversion time: 0.000014s int: calculation time: 0.009037s conversion time: 0.006705s --- pyperformance/data-files/benchmarks/MANIFEST | 3 +-- .../bm_decimal_factorial/run_benchmark.py | 21 +++++++++++++------ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/pyperformance/data-files/benchmarks/MANIFEST b/pyperformance/data-files/benchmarks/MANIFEST index 69aea2b9..202de64c 100644 --- a/pyperformance/data-files/benchmarks/MANIFEST +++ b/pyperformance/data-files/benchmarks/MANIFEST @@ -37,8 +37,7 @@ chaos comprehensions crypto_pyaes dask -# FIXME: this doesn't work with `_pydecimal` -# decimal_factorial +decimal_factorial decimal_pi deepcopy deltablue diff --git a/pyperformance/data-files/benchmarks/bm_decimal_factorial/run_benchmark.py b/pyperformance/data-files/benchmarks/bm_decimal_factorial/run_benchmark.py index dceba16b..af737bc7 100644 --- a/pyperformance/data-files/benchmarks/bm_decimal_factorial/run_benchmark.py +++ b/pyperformance/data-files/benchmarks/bm_decimal_factorial/run_benchmark.py @@ -4,6 +4,8 @@ - 2024-06-14: Michael Droettboom copied this from Modules/_decimal/tests/bench.py in the CPython source and adapted to use pyperf. +- 2026-02-22: Sergey B Kirpichev adapted context settings and tested + values to support also pure-Python decimal module. """ # Original copyright notice in CPython source: @@ -13,8 +15,11 @@ # Modified and extended by Stefan Krah. # - import decimal +try: + import _decimal +except ImportError: + _decimal = None import pyperf @@ -33,12 +38,16 @@ def factorial(n, m): def bench_decimal_factorial(): c = decimal.getcontext() - c.prec = decimal.MAX_PREC - c.Emax = decimal.MAX_EMAX - c.Emin = decimal.MIN_EMIN + if _decimal: + c.prec = decimal.MAX_PREC + c.Emax = decimal.MAX_EMAX + c.Emin = decimal.MIN_EMIN + data = [10000, 100000] + else: + c.prec = 20000 # Should be enough to hold largest factorial exactly. + data = [1000, 5000] - for n in [10000, 100000]: - # C version of decimal + for n in data: _ = factorial(decimal.Decimal(n), 0) From b1847bb4dfaa4c429394bd56663f88b03141c19f Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Sun, 22 Feb 2026 04:01:14 +0300 Subject: [PATCH 5/6] + increase_int_max_str_digits() decorator --- .../bm_decimal_factorial/run_benchmark.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/pyperformance/data-files/benchmarks/bm_decimal_factorial/run_benchmark.py b/pyperformance/data-files/benchmarks/bm_decimal_factorial/run_benchmark.py index af737bc7..3e77b45a 100644 --- a/pyperformance/data-files/benchmarks/bm_decimal_factorial/run_benchmark.py +++ b/pyperformance/data-files/benchmarks/bm_decimal_factorial/run_benchmark.py @@ -5,7 +5,8 @@ Modules/_decimal/tests/bench.py in the CPython source and adapted to use pyperf. - 2026-02-22: Sergey B Kirpichev adapted context settings and tested - values to support also pure-Python decimal module. + values to support also pure-Python decimal module, copy + increase_int_max_str_digits() decorator. """ # Original copyright notice in CPython source: @@ -20,7 +21,7 @@ import _decimal except ImportError: _decimal = None - +import sys import pyperf @@ -36,6 +37,20 @@ def factorial(n, m): return factorial(n, (n + m) // 2) * factorial((n + m) // 2 + 1, m) +def increase_int_max_str_digits(maxdigits): + def _increase_int_max_str_digits(func, maxdigits=maxdigits): + @wraps(func) + def wrapper(*args, **kwargs): + previous_int_limit = sys.get_int_max_str_digits() + sys.set_int_max_str_digits(maxdigits) + ans = func(*args, **kwargs) + sys.set_int_max_str_digits(previous_int_limit) + return ans + return wrapper + return _increase_int_max_str_digits + + +@increase_int_max_str_digits(maxdigits=10000000) def bench_decimal_factorial(): c = decimal.getcontext() if _decimal: From d17e2d802df6d11358af94cca095574c2f17e2fc Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Sun, 22 Feb 2026 04:10:13 +0300 Subject: [PATCH 6/6] +1 --- .../data-files/benchmarks/bm_decimal_factorial/run_benchmark.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pyperformance/data-files/benchmarks/bm_decimal_factorial/run_benchmark.py b/pyperformance/data-files/benchmarks/bm_decimal_factorial/run_benchmark.py index 3e77b45a..132cd6fb 100644 --- a/pyperformance/data-files/benchmarks/bm_decimal_factorial/run_benchmark.py +++ b/pyperformance/data-files/benchmarks/bm_decimal_factorial/run_benchmark.py @@ -22,6 +22,7 @@ except ImportError: _decimal = None import sys +from functools import wraps import pyperf