Skip to content

Enable decimal benchmarks - #491

Open
skirpichev wants to merge 12 commits into
python:mainfrom
skirpichev:enable-decimal_pi
Open

skirpichev wants to merge 12 commits into
python:mainfrom
skirpichev:enable-decimal_pi

Conversation

@skirpichev

Copy link
Copy Markdown
Member

They were not added here in #341 for no reasons.
Tune bench_decimal_factorial() to support pure-Python decimal module on PyPy. See also python/cpython#155198.

Co-authored-by: Hugo van Kemenade 1324225+hugovk@users.noreply.github.com

Closes #473

// N.B.: this is essentially a rebirth of #453.

They were not added here in python#341 for no reasons.
Tune bench_decimal_factorial() to support pure-Python decimal
module on PyPy.  See also python/cpython#155198.

Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>

Closes python#473

@StanFromIreland StanFromIreland left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

decimal_factorial now runs two different workloads under one name, 10000! and 100000! at MAX_PREC with _decimal, but 1000! and 5000! at prec = 20000 without it, so results from CPython and PyPy (or a CPython built without _decimal) aren't comparable and nothing in the output says which one ran.

@skirpichev

Copy link
Copy Markdown
Member Author

results from CPython and PyPy (or a CPython built without _decimal) aren't comparable and nothing in the output says which one ran.

Fair enough.

Here the whole picture. Without the patch for python/cpython#140036 we can't just set context's prec to MAX_PREC on pure-Python module. Instead we should set it to something "big enough". For 100000 it will be:

>>> from mpmath import log10, fac
>>> from mpmath import log10, fac, ceil
>>> ceil(log10(fac(100000)))
mpf('456574.0')

And with such explicit big settings for prec - computations are very slow. E.g. for prec=50000 on my system it's around 81sec. For 10x prec it will be ~hour (I canceled run after several minutes).

But with the patch (and prec = MAX_PREC) I've on CPython:

>>> start_calc = time.time(); x = factorial(P.Decimal(10000), 0); end_calc = time.time(); end_calc - start_calc
13.344191074371338
>>> start_calc = time.time(); x = factorial(C.Decimal(10000), 0); end_calc = time.time(); end_calc - start_calc
0.5661253929138184
>>> start_calc = time.time(); x = factorial(C.Decimal(100000), 0); end_calc = time.time(); end_calc - start_calc
4.615884304046631
>>> start_calc = time.time(); x = factorial(P.Decimal(100000), 0); end_calc = time.time(); end_calc - start_calc
128.01505613327026

And with the patch on the latest PyPy3.12:

>>>> start_calc = time.time(); x = factorial(P.Decimal(1000), 0); end_calc = time.time(); end_calc - start_calc
0.6504781246185303
>>>> start_calc = time.time(); x = factorial(P.Decimal(10000), 0); end_calc = time.time(); end_calc - start_calc
1.0633630752563477
>>>> start_calc = time.time(); x = factorial(P.Decimal(100000), 0); end_calc = time.time(); end_calc - start_calc
5.9123358726501465

I think we should convince PyPy people to apply the patch. Then make settings same for all implementations (C and pure-Python): data = [10000, 100000] and c.prec = decimal.MAX_PREC.

Or we can treat python/cpython#140036 as a security issue... Though, it would be a very liberal interpretation.

@StanFromIreland

Copy link
Copy Markdown
Member

The fix for python/cpython#140036 won't be picked up by PyPy's stdlib sync. Could you open a PR with the _pydecimal change against PyPy's py3.12 branch? I can merge it there. For this PR, I'd then go with one workload everywhere. Until a PyPy release ships the fix, decimal_factorial would need unsupported_implementations = ["pypy"] in its pyproject.toml (like bm_dask).

CC @mattip

Also drop all custom settings for pure-Python module.
@skirpichev

Copy link
Copy Markdown
Member Author

@StanFromIreland StanFromIreland left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update the changelog and add docs.

@StanFromIreland

Copy link
Copy Markdown
Member

Please update the changelog and add docs.

?

@skirpichev

Copy link
Copy Markdown
Member Author

Please update the changelog and add docs.

Oh, sorry, I miss that comment. PR updated, please review.

Comment thread doc/benchmarks.rst Resolved
Comment thread doc/benchmarks.rst Resolved
Comment thread doc/changelog.rst Resolved
skirpichev and others added 4 commits December 2, 2026 00:27
Co-authored-by: Stan Ulbrych <stan@python.org>
Co-authored-by: Stan Ulbrych <stan@python.org>
@skirpichev skirpichev changed the title Enable decimal benchmarks in the suite (MANIFEST) Dec 2, 2026
Comment thread doc/benchmarks.rst Resolved
Comment thread doc/changelog.rst Resolved
Co-authored-by: Stan Ulbrych <stan@python.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants