Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(134)

## Side by Side Diff: Modules/_decimal/tests/bench.py

Issue 7652: Merge C version of decimal into py3k.
Patch Set: Created 7 years, 10 months ago
 Left: Base Patch Set 1: None Patch Set 2: None Patch Set 3: None Patch Set 4: None Patch Set 5: None Patch Set 6: None Patch Set 7: None Patch Set 8: None Patch Set 9: None Patch Set 10: None Patch Set 11: None Patch Set 12: None Right: Patch Set 1: None Patch Set 2: None Patch Set 3: None Patch Set 4: None Patch Set 5: None Patch Set 6: None Patch Set 7: None Patch Set 8: None Patch Set 9: None Patch Set 10: None Patch Set 11: None Patch Set 12: None
« no previous file with comments | « Modules/_decimal/README.txt ('k') | Modules/_decimal/tests/deccheck.py » ('j') | no next file with comments »
OLDNEW
(Empty)
1 #!/usr/bin/env python
2
3 #
5 # Modified and extended by Stefan Krah.
6 #
7
8 # Usage: ../../../python bench.py
9
10
11 import time
12 from math import log, ceil
13 from test.support import import_fresh_module
14
15 C = import_fresh_module('decimal', fresh=['_decimal'])
16 P = import_fresh_module('decimal', blocked=['_decimal'])
17
18
19 # Pi function from the decimal.py documentation
20 def pi_float(prec):
21 """native float"""
22 lasts, t, s, n, na, d, da = 0, 3.0, 3, 1, 0, 0, 24
23 while s != lasts:
24 lasts = s
25 n, na = n+na, na+8
26 d, da = d+da, da+32
27 t = (t * n) / d
28 s += t
29 return s
30
31 def pi_cdecimal(prec):
32 """cdecimal"""
33 C.getcontext().prec = prec
34 D = C.Decimal
35 lasts, t, s, n, na, d, da = D(0), D(3), D(3), D(1), D(0), D(0), D(24)
36 while s != lasts:
37 lasts = s
38 n, na = n+na, na+8
39 d, da = d+da, da+32
40 t = (t * n) / d
41 s += t
42 return s
43
44 def pi_decimal(prec):
45 """decimal"""
46 P.getcontext().prec = prec
47 D = P.Decimal
48 lasts, t, s, n, na, d, da = D(0), D(3), D(3), D(1), D(0), D(0), D(24)
49 while s != lasts:
50 lasts = s
51 n, na = n+na, na+8
52 d, da = d+da, da+32
53 t = (t * n) / d
54 s += t
55 return s
56
57 def factorial(n, m):
58 if (n > m):
59 return factorial(m, n)
60 elif m == 0:
61 return 1
62 elif n == m:
63 return n
64 else:
65 return factorial(n, (n+m)//2) * factorial((n+m)//2 + 1, m)
66
67
68 print("\n# ===================================================================== =")
69 print("# Calculating pi, 10000 iterations")
70 print("# ======================================================================\ n")
71
72 for prec in [9, 19]:
73 print("\nPrecision: %d decimal digits\n" % prec)
74 for func in [pi_float, pi_cdecimal, pi_decimal]:
75 start = time.time()
76 for i in range(10000):
77 x = func(prec)
78 print("%s:" % func.__name__.replace("pi_", ""))
79 print("result: %s" % str(x))
80 print("time: %fs\n" % (time.time()-start))
81
82
83 print("\n# ===================================================================== =")
84 print("# Factorial")
85 print("# ======================================================================\ n")
86
87 C.getcontext().prec = C.MAX_PREC
88
89 for n in [100000, 1000000]:
90
91 print("n = %d\n" % n)
92
93 # C version of decimal
94 start_calc = time.time()
95 x = factorial(C.Decimal(n), 0)
96 end_calc = time.time()
97 start_conv = time.time()
98 sx = str(x)
99 end_conv = time.time()
100 print("cdecimal:")
101 print("calculation time: %fs" % (end_calc-start_calc))
102 print("conversion time: %fs\n" % (end_conv-start_conv))
103
104 # Python integers
105 start_calc = time.time()
106 y = factorial(n, 0)
107 end_calc = time.time()
108 start_conv = time.time()
109 sy = str(y)
110 end_conv = time.time()
111
112 print("int:")
113 print("calculation time: %fs" % (end_calc-start_calc))
114 print("conversion time: %fs\n\n" % (end_conv-start_conv))
115
116 assert(sx == sy)
OLDNEW
« no previous file with comments | « Modules/_decimal/README.txt ('k') | Modules/_decimal/tests/deccheck.py » ('j') | no next file with comments »

This is Rietveld 894c83f36cb7+