Skip to content

Commit 9d19768

Browse files
authored
Add benchmarks for the decimal module (#341)
These currently exist in the [CPython source](https://github.com/python/cpython/blob/main/Modules/_decimal/tests/bench.py), but it would be good to have them here for continuous benchmarking.
1 parent c91657e commit 9d19768

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[project]
2+
name = "pyperformance_bm_decimal_factorial"
3+
requires-python = ">=3.8"
4+
dependencies = ["pyperf"]
5+
urls = {repository = "https://github.com/python/pyperformance"}
6+
dynamic = ["version"]
7+
8+
[tool.pyperformance]
9+
name = "decimal_factorial"
10+
tags = "decimal"
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"""
2+
Calculate `factorial` using the decimal module.
3+
4+
- 2024-06-14: Michael Droettboom copied this from
5+
Modules/_decimal/tests/bench.py in the CPython source and adapted to use
6+
pyperf.
7+
"""
8+
9+
# Original copyright notice in CPython source:
10+
11+
#
12+
# Copyright (C) 2001-2012 Python Software Foundation. All Rights Reserved.
13+
# Modified and extended by Stefan Krah.
14+
#
15+
16+
17+
import decimal
18+
19+
20+
import pyperf
21+
22+
23+
def factorial(n, m):
24+
if n > m:
25+
return factorial(m, n)
26+
elif m == 0:
27+
return 1
28+
elif n == m:
29+
return n
30+
else:
31+
return factorial(n, (n + m) // 2) * factorial((n + m) // 2 + 1, m)
32+
33+
34+
def bench_decimal_factorial():
35+
c = decimal.getcontext()
36+
c.prec = decimal.MAX_PREC
37+
c.Emax = decimal.MAX_EMAX
38+
c.Emin = decimal.MIN_EMIN
39+
40+
for n in [10000, 100000]:
41+
# C version of decimal
42+
_ = factorial(decimal.Decimal(n), 0)
43+
44+
45+
if __name__ == "__main__":
46+
runner = pyperf.Runner()
47+
runner.metadata["description"] = "decimal_factorial benchmark"
48+
49+
args = runner.parse_args()
50+
runner.bench_func("decimal_factorial", bench_decimal_factorial)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[project]
2+
name = "pyperformance_bm_decimal_pi"
3+
requires-python = ">=3.8"
4+
dependencies = ["pyperf"]
5+
urls = {repository = "https://github.com/python/pyperformance"}
6+
dynamic = ["version"]
7+
8+
[tool.pyperformance]
9+
name = "decimal_pi"
10+
tags = "decimal"
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"""
2+
Calculate `pi` using the decimal module.
3+
4+
The `pidigits` benchmark does a similar thing using regular (long) ints.
5+
6+
- 2024-06-14: Michael Droettboom copied this from
7+
Modules/_decimal/tests/bench.py in the CPython source and adapted to use
8+
pyperf.
9+
"""
10+
11+
# Original copyright notice in CPython source:
12+
13+
#
14+
# Copyright (C) 2001-2012 Python Software Foundation. All Rights Reserved.
15+
# Modified and extended by Stefan Krah.
16+
#
17+
18+
19+
import decimal
20+
21+
22+
import pyperf
23+
24+
25+
def pi_decimal():
26+
"""decimal"""
27+
D = decimal.Decimal
28+
lasts, t, s, n, na, d, da = D(0), D(3), D(3), D(1), D(0), D(0), D(24)
29+
while s != lasts:
30+
lasts = s
31+
n, na = n + na, na + 8
32+
d, da = d + da, da + 32
33+
t = (t * n) / d
34+
s += t
35+
return s
36+
37+
38+
def bench_decimal_pi():
39+
for prec in [9, 19]:
40+
decimal.getcontext().prec = prec
41+
for _ in range(10000):
42+
_ = pi_decimal()
43+
44+
45+
if __name__ == "__main__":
46+
runner = pyperf.Runner()
47+
runner.metadata["description"] = "decimal_pi benchmark"
48+
49+
args = runner.parse_args()
50+
runner.bench_func("decimal_pi", bench_decimal_pi)

0 commit comments

Comments
 (0)