1010
1111import time
1212from math import log , ceil
13- from test .support import import_fresh_module
13+ try :
14+ from test .support import import_fresh_module
15+ except ImportError :
16+ from test .test_support import import_fresh_module
1417
1518C = import_fresh_module ('decimal' , fresh = ['_decimal' ])
1619P = import_fresh_module ('decimal' , blocked = ['_decimal' ])
@@ -69,9 +72,13 @@ def factorial(n, m):
6972
7073for prec in [9 , 19 ]:
7174 print ("\n Precision: %d decimal digits\n " % prec )
72- for func in [pi_float , pi_cdecimal , pi_decimal ]:
75+ to_benchmark = [pi_float , pi_decimal ]
76+ if C is not None :
77+ to_benchmark .append (pi_cdecimal )
78+ for func in to_benchmark :
7379 start = time .time ()
74- C .getcontext ().prec = prec
80+ if C is not None :
81+ C .getcontext ().prec = prec
7582 P .getcontext ().prec = prec
7683 for i in range (10000 ):
7784 x = func ()
@@ -84,25 +91,27 @@ def factorial(n, m):
8491print ("# Factorial" )
8592print ("# ======================================================================\n " )
8693
87- c = C .getcontext ()
88- c .prec = C .MAX_PREC
89- c .Emax = C .MAX_EMAX
90- c .Emin = C .MIN_EMIN
94+ if C is not None :
95+ c = C .getcontext ()
96+ c .prec = C .MAX_PREC
97+ c .Emax = C .MAX_EMAX
98+ c .Emin = C .MIN_EMIN
9199
92100for n in [100000 , 1000000 ]:
93101
94102 print ("n = %d\n " % n )
95103
96- # C version of decimal
97- start_calc = time .time ()
98- x = factorial (C .Decimal (n ), 0 )
99- end_calc = time .time ()
100- start_conv = time .time ()
101- sx = str (x )
102- end_conv = time .time ()
103- print ("cdecimal:" )
104- print ("calculation time: %fs" % (end_calc - start_calc ))
105- print ("conversion time: %fs\n " % (end_conv - start_conv ))
104+ if C is not None :
105+ # C version of decimal
106+ start_calc = time .time ()
107+ x = factorial (C .Decimal (n ), 0 )
108+ end_calc = time .time ()
109+ start_conv = time .time ()
110+ sx = str (x )
111+ end_conv = time .time ()
112+ print ("cdecimal:" )
113+ print ("calculation time: %fs" % (end_calc - start_calc ))
114+ print ("conversion time: %fs\n " % (end_conv - start_conv ))
106115
107116 # Python integers
108117 start_calc = time .time ()
@@ -116,4 +125,5 @@ def factorial(n, m):
116125 print ("calculation time: %fs" % (end_calc - start_calc ))
117126 print ("conversion time: %fs\n \n " % (end_conv - start_conv ))
118127
119- assert (sx == sy )
128+ if C is not None :
129+ assert (sx == sy )
0 commit comments