Skip to content

Commit 243a809

Browse files
committed
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
1 parent 9fb5802 commit 243a809

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

pyperformance/data-files/benchmarks/MANIFEST

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ chaos <local>
3737
comprehensions <local>
3838
crypto_pyaes <local>
3939
dask <local>
40-
# FIXME: this doesn't work with `_pydecimal`
41-
# decimal_factorial <local>
40+
decimal_factorial <local>
4241
decimal_pi <local>
4342
deepcopy <local>
4443
deltablue <local>

pyperformance/data-files/benchmarks/bm_decimal_factorial/run_benchmark.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
- 2024-06-14: Michael Droettboom copied this from
55
Modules/_decimal/tests/bench.py in the CPython source and adapted to use
66
pyperf.
7+
- 2026-02-22: Sergey B Kirpichev adapted context settings and tested
8+
values to support also pure-Python decimal module.
79
"""
810

911
# Original copyright notice in CPython source:
@@ -13,8 +15,11 @@
1315
# Modified and extended by Stefan Krah.
1416
#
1517

16-
1718
import decimal
19+
try:
20+
import _decimal
21+
except ImportError:
22+
_decimal = None
1823

1924

2025
import pyperf
@@ -33,12 +38,16 @@ def factorial(n, m):
3338

3439
def bench_decimal_factorial():
3540
c = decimal.getcontext()
36-
c.prec = decimal.MAX_PREC
37-
c.Emax = decimal.MAX_EMAX
38-
c.Emin = decimal.MIN_EMIN
41+
if _decimal:
42+
c.prec = decimal.MAX_PREC
43+
c.Emax = decimal.MAX_EMAX
44+
c.Emin = decimal.MIN_EMIN
45+
data = [10000, 100000]
46+
else:
47+
c.prec = 20000 # Should be enough to hold largest factorial exactly.
48+
data = [1000, 5000]
3949

40-
for n in [10000, 100000]:
41-
# C version of decimal
50+
for n in data:
4251
_ = factorial(decimal.Decimal(n), 0)
4352

4453

0 commit comments

Comments
 (0)