Files
RustPython/benches/benchmarks/decimal_factorial.py
T
Lee Dogeon 99a63f92aa benchmarks: add pyperformance workloads (#8633)
Add dependency-free CPU-bound workloads adapted from pyperformance 1.14.0 to cover interpreter, standard-library serialization, regex, numeric, and garbage-collection paths under the existing Criterion and CodSpeed harnesses. Keep inputs deterministic and scale expensive cases for simulation while checking that benchmark execution does not leak interpreter state.

Assisted-by: Codex:gpt-5.6-sol
2026-09-01 10:03:29 +09:00

43 lines
1.0 KiB
Python

# Adapted from python/pyperformance 1.14.0 (decimal_factorial).
"""
Calculate `factorial` using the decimal module.
- 2024-06-14: Michael Droettboom copied this from
Modules/_decimal/tests/bench.py in the CPython source and adapted to use
pyperf.
"""
# Original copyright notice in CPython source:
#
# Copyright (C) 2001-2012 Python Software Foundation. All Rights Reserved.
# Modified and extended by Stefan Krah.
#
import decimal
def factorial(n, m):
if n > m:
return factorial(m, n)
elif m == 0:
return 1
elif n == m:
return n
else:
return factorial(n, (n + m) // 2) * factorial((n + m) // 2 + 1, m)
def bench_decimal_factorial():
# The upstream 10,000! and 100,000! inputs are reduced for simulation,
# while retaining enough precision to exercise large Decimal arithmetic.
with decimal.localcontext() as context:
context.prec = 3000
result = factorial(decimal.Decimal(1000), 0)
assert len(result.as_tuple().digits) == 2568
bench_decimal_factorial()