Debian Codegraph @

Modules/_decimal/tests/bench.py

raw
1 #
2 # Copyright (C) 2001 Python Software Foundation. All Rights Reserved.
3 # Modified and extended by Stefan Krah.
4 #
5
6 # Usage: ../../../python bench.py
7
8
9 import time
10 import sys
11 from functools import wraps
12 from test.support.import_helper import import_fresh_module
13
14 C = import_fresh_module('decimal', fresh=['_decimal'])
15 P = import_fresh_module('decimal', blocked=['_decimal'])
16
17 #
18 # NOTE: This is the pi function from the decimal documentation, modified
19 # for benchmarking purposes. Since floats do not have a context, the higher
20 # intermediate precision from the original is NOT used, so the modified
21 # algorithm only gives an approximation to the correctly rounded result.
22 # For serious use, refer to the documentation or the appropriate literature.
23 #
24 def pi_float():
25 """native float"""
26 lasts, t, s, n, na, d, da = 0, 3.0, 3, 1, 0, 0, 24
27 while s != lasts:
28 lasts = s
29 n, na = n+na, na+8
30 d, da = d+da, da+32
31 t = (t * n) / d
32 s += t
33 return s
34
35 def pi_cdecimal():
36 """cdecimal"""
37 D = C.Decimal
38 lasts, t, s, n, na, d, da = D(0), D(3), D(3), D(1), D(0), D(0), D(24)
39 while s != lasts:
40 lasts = s
41 n, na = n+na, na+8
42 d, da = d+da, da+32
43 t = (t * n) / d
44 s += t
45 return s
46
47 def pi_decimal():
48 """decimal"""
49 D = P.Decimal
50 lasts, t, s, n, na, d, da = D(0), D(3), D(3), D(1), D(0), D(0), D(24)
51 while s != lasts:
52 lasts = s
53 n, na = n+na, na+8
54 d, da = d+da, da+32
55 t = (t * n) / d
56 s += t
57 return s
58
59 def factorial(n, m):
60 if (n > m):
61 return factorial(m, n)
62 elif m == 0:
63 return 1
64 elif n == m:
65 return n
66 else:
67 return factorial(n, (n+m)//2) * factorial((n+m)//2 + 1, m)
68
69 # Fix failed test cases caused by CVE-2020-10735 patch.
70 # See gh-95778 for details.
71 def increase_int_max_str_digits(maxdigits):
72 def _increase_int_max_str_digits(func, maxdigits=maxdigits):
73 @wraps(func)
74 def wrapper(*args, **kwargs):
75 previous_int_limit = sys.get_int_max_str_digits()
76 sys.set_int_max_str_digits(maxdigits)
77 ans = func(*args, **kwargs)
78 sys.set_int_max_str_digits(previous_int_limit)
79 return ans
80 return wrapper
81 return _increase_int_max_str_digits
82
83 def test_calc_pi():
84 print("\n# ======================================================================")
85 print("# Calculating pi, 10000 iterations")
86 print("# ======================================================================\n")
87
88 to_benchmark = [pi_float, pi_decimal]
89 if C is not None:
90 to_benchmark.insert(1, pi_cdecimal)
91
92 for prec in [9, 19]:
93 print("\nPrecision: %d decimal digits\n" % prec)
94 for func in to_benchmark:
95 start = time.time()
96 if C is not None:
97 C.getcontext().prec = prec
98 P.getcontext().prec = prec
99 for i in range(10000):
100 x = func()
101 print("%s:" % func.__name__.replace("pi_", ""))
102 print("result: %s" % str(x))
103 print("time: %fs\n" % (time.time()-start))
104
105 @increase_int_max_str_digits(maxdigits=10000000)
106 def test_factorial():
107 print("\n# ======================================================================")
108 print("# Factorial")
109 print("# ======================================================================\n")
110
111 if C is not None:
112 c = C.getcontext()
113 c.prec = C.MAX_PREC
114 c.Emax = C.MAX_EMAX
115 c.Emin = C.MIN_EMIN
116
117 for n in [100000, 1000000]:
118
119 print("n = %d\n" % n)
120
121 if C is not None:
122 # C version of decimal
123 start_calc = time.time()
124 x = factorial(C.Decimal(n), 0)
125 end_calc = time.time()
126 start_conv = time.time()
127 sx = str(x)
128 end_conv = time.time()
129 print("cdecimal:")
130 print("calculation time: %fs" % (end_calc-start_calc))
131 print("conversion time: %fs\n" % (end_conv-start_conv))
132
133 # Python integers
134 start_calc = time.time()
135 y = factorial(n, 0)
136 end_calc = time.time()
137 start_conv = time.time()
138 sy = str(y)
139 end_conv = time.time()
140
141 print("int:")
142 print("calculation time: %fs" % (end_calc-start_calc))
143 print("conversion time: %fs\n\n" % (end_conv-start_conv))
144
145 if C is not None:
146 assert(sx == sy)
147
148 if __name__ == "__main__":
149 test_calc_pi()
150 test_factorial()