ยปCore Development>Code coverage>Modules/_decimal/tests/bench.py

# Python code coverage for Modules/_decimal/tests/bench.py

#countcontent
1n/a#
3n/a# Modified and extended by Stefan Krah.
4n/a#
5n/a
6n/a# Usage: ../../../python bench.py
7n/a
8n/a
9n/aimport time
10n/afrom math import log, ceil
11n/atry:
12n/a from test.support import import_fresh_module
13n/aexcept ImportError:
14n/a from test.test_support import import_fresh_module
15n/a
16n/aC = import_fresh_module('decimal', fresh=['_decimal'])
17n/aP = import_fresh_module('decimal', blocked=['_decimal'])
18n/a
19n/a#
20n/a# NOTE: This is the pi function from the decimal documentation, modified
21n/a# for benchmarking purposes. Since floats do not have a context, the higher
22n/a# intermediate precision from the original is NOT used, so the modified
23n/a# algorithm only gives an approximation to the correctly rounded result.
24n/a# For serious use, refer to the documentation or the appropriate literature.
25n/a#
27n/a """native float"""
28n/a lasts, t, s, n, na, d, da = 0, 3.0, 3, 1, 0, 0, 24
29n/a while s != lasts:
30n/a lasts = s
31n/a n, na = n+na, na+8
32n/a d, da = d+da, da+32
33n/a t = (t * n) / d
34n/a s += t
35n/a return s
36n/a
38n/a """cdecimal"""
39n/a D = C.Decimal
40n/a lasts, t, s, n, na, d, da = D(0), D(3), D(3), D(1), D(0), D(0), D(24)
41n/a while s != lasts:
42n/a lasts = s
43n/a n, na = n+na, na+8
44n/a d, da = d+da, da+32
45n/a t = (t * n) / d
46n/a s += t
47n/a return s
48n/a
50n/a """decimal"""
51n/a D = P.Decimal
52n/a lasts, t, s, n, na, d, da = D(0), D(3), D(3), D(1), D(0), D(0), D(24)
53n/a while s != lasts:
54n/a lasts = s
55n/a n, na = n+na, na+8
56n/a d, da = d+da, da+32
57n/a t = (t * n) / d
58n/a s += t
59n/a return s
60n/a
62n/a if (n > m):
63n/a return factorial(m, n)
64n/a elif m == 0:
65n/a return 1
66n/a elif n == m:
67n/a return n
68n/a else:
69n/a return factorial(n, (n+m)//2) * factorial((n+m)//2 + 1, m)
70n/a
71n/a
72n/aprint("\n# ======================================================================")
73n/aprint("# Calculating pi, 10000 iterations")
74n/aprint("# ======================================================================\n")
75n/a
76n/ato_benchmark = [pi_float, pi_decimal]
77n/aif C is not None:
78n/a to_benchmark.insert(1, pi_cdecimal)
79n/a
80n/afor prec in [9, 19]:
81n/a print("\nPrecision: %d decimal digits\n" % prec)
82n/a for func in to_benchmark:
83n/a start = time.time()
84n/a if C is not None:
85n/a C.getcontext().prec = prec
86n/a P.getcontext().prec = prec
87n/a for i in range(10000):
88n/a x = func()
89n/a print("%s:" % func.__name__.replace("pi_", ""))
90n/a print("result: %s" % str(x))
91n/a print("time: %fs\n" % (time.time()-start))
92n/a
93n/a
94n/aprint("\n# ======================================================================")
95n/aprint("# Factorial")
96n/aprint("# ======================================================================\n")
97n/a
98n/aif C is not None:
99n/a c = C.getcontext()
100n/a c.prec = C.MAX_PREC
101n/a c.Emax = C.MAX_EMAX
102n/a c.Emin = C.MIN_EMIN
103n/a
104n/afor n in [100000, 1000000]:
105n/a
106n/a print("n = %d\n" % n)
107n/a
108n/a if C is not None:
109n/a # C version of decimal
110n/a start_calc = time.time()
111n/a x = factorial(C.Decimal(n), 0)
112n/a end_calc = time.time()
113n/a start_conv = time.time()
114n/a sx = str(x)
115n/a end_conv = time.time()
116n/a print("cdecimal:")
117n/a print("calculation time: %fs" % (end_calc-start_calc))
118n/a print("conversion time: %fs\n" % (end_conv-start_conv))
119n/a
120n/a # Python integers
121n/a start_calc = time.time()
122n/a y = factorial(n, 0)
123n/a end_calc = time.time()
124n/a start_conv = time.time()
125n/a sy = str(y)
126n/a end_conv = time.time()
127n/a
128n/a print("int:")
129n/a print("calculation time: %fs" % (end_calc-start_calc))
130n/a print("conversion time: %fs\n\n" % (end_conv-start_conv))
131n/a
132n/a if C is not None:
133n/a assert(sx == sy)