From d266c4451f25840651d94b6e68b05a0e28dd2a03 Mon Sep 17 00:00:00 2001 From: Brett Cannon Date: Tue, 25 Sep 2012 10:23:47 -0400 Subject: [PATCH 1/3] Make the decimal bench file run under Python 2.7. --- Modules/_decimal/tests/bench.py | 46 ++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/Modules/_decimal/tests/bench.py b/Modules/_decimal/tests/bench.py index 01f851c8195..ce864b4f114 100644 --- a/Modules/_decimal/tests/bench.py +++ b/Modules/_decimal/tests/bench.py @@ -10,7 +10,10 @@ import time from math import log, ceil -from test.support import import_fresh_module +try: + from test.support import import_fresh_module +except ImportError: + from test.test_support import import_fresh_module C = import_fresh_module('decimal', fresh=['_decimal']) P = import_fresh_module('decimal', blocked=['_decimal']) @@ -69,9 +72,13 @@ print("# ======================================================================\ for prec in [9, 19]: print("\nPrecision: %d decimal digits\n" % prec) - for func in [pi_float, pi_cdecimal, pi_decimal]: + to_benchmark = [pi_float, pi_decimal] + if C is not None: + to_benchmark.append(pi_cdecimal) + for func in to_benchmark: start = time.time() - C.getcontext().prec = prec + if C is not None: + C.getcontext().prec = prec P.getcontext().prec = prec for i in range(10000): x = func() @@ -84,25 +91,27 @@ print("\n# ===================================================================== print("# Factorial") print("# ======================================================================\n") -c = C.getcontext() -c.prec = C.MAX_PREC -c.Emax = C.MAX_EMAX -c.Emin = C.MIN_EMIN +if C is not None: + c = C.getcontext() + c.prec = C.MAX_PREC + c.Emax = C.MAX_EMAX + c.Emin = C.MIN_EMIN for n in [100000, 1000000]: print("n = %d\n" % n) - # C version of decimal - start_calc = time.time() - x = factorial(C.Decimal(n), 0) - end_calc = time.time() - start_conv = time.time() - sx = str(x) - end_conv = time.time() - print("cdecimal:") - print("calculation time: %fs" % (end_calc-start_calc)) - print("conversion time: %fs\n" % (end_conv-start_conv)) + if C is not None: + # C version of decimal + start_calc = time.time() + x = factorial(C.Decimal(n), 0) + end_calc = time.time() + start_conv = time.time() + sx = str(x) + end_conv = time.time() + print("cdecimal:") + print("calculation time: %fs" % (end_calc-start_calc)) + print("conversion time: %fs\n" % (end_conv-start_conv)) # Python integers start_calc = time.time() @@ -116,4 +125,5 @@ for n in [100000, 1000000]: print("calculation time: %fs" % (end_calc-start_calc)) print("conversion time: %fs\n\n" % (end_conv-start_conv)) - assert(sx == sy) + if C is not None: + assert(sx == sy) From 63092fe0ea5779aea6c64020290863d125de7fa2 Mon Sep 17 00:00:00 2001 From: Brett Cannon Date: Tue, 25 Sep 2012 10:25:41 -0400 Subject: [PATCH 2/3] Fix whitespace. --- Modules/_decimal/tests/bench.py | 36 ++++++++++++++++----------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/Modules/_decimal/tests/bench.py b/Modules/_decimal/tests/bench.py index ce864b4f114..fef7a220196 100644 --- a/Modules/_decimal/tests/bench.py +++ b/Modules/_decimal/tests/bench.py @@ -11,9 +11,9 @@ import time from math import log, ceil try: - from test.support import import_fresh_module + from test.support import import_fresh_module except ImportError: - from test.test_support import import_fresh_module + from test.test_support import import_fresh_module C = import_fresh_module('decimal', fresh=['_decimal']) P = import_fresh_module('decimal', blocked=['_decimal']) @@ -74,7 +74,7 @@ for prec in [9, 19]: print("\nPrecision: %d decimal digits\n" % prec) to_benchmark = [pi_float, pi_decimal] if C is not None: - to_benchmark.append(pi_cdecimal) + to_benchmark.append(pi_cdecimal) for func in to_benchmark: start = time.time() if C is not None: @@ -92,26 +92,26 @@ print("# Factorial") print("# ======================================================================\n") if C is not None: - c = C.getcontext() - c.prec = C.MAX_PREC - c.Emax = C.MAX_EMAX - c.Emin = C.MIN_EMIN + c = C.getcontext() + c.prec = C.MAX_PREC + c.Emax = C.MAX_EMAX + c.Emin = C.MIN_EMIN for n in [100000, 1000000]: print("n = %d\n" % n) if C is not None: - # C version of decimal - start_calc = time.time() - x = factorial(C.Decimal(n), 0) - end_calc = time.time() - start_conv = time.time() - sx = str(x) - end_conv = time.time() - print("cdecimal:") - print("calculation time: %fs" % (end_calc-start_calc)) - print("conversion time: %fs\n" % (end_conv-start_conv)) + # C version of decimal + start_calc = time.time() + x = factorial(C.Decimal(n), 0) + end_calc = time.time() + start_conv = time.time() + sx = str(x) + end_conv = time.time() + print("cdecimal:") + print("calculation time: %fs" % (end_calc-start_calc)) + print("conversion time: %fs\n" % (end_conv-start_conv)) # Python integers start_calc = time.time() @@ -126,4 +126,4 @@ for n in [100000, 1000000]: print("conversion time: %fs\n\n" % (end_conv-start_conv)) if C is not None: - assert(sx == sy) + assert(sx == sy) From 6cf50c5b1deb7d31debe345e2d18f8402d00d1f4 Mon Sep 17 00:00:00 2001 From: Brett Cannon Date: Tue, 25 Sep 2012 10:26:15 -0400 Subject: [PATCH 3/3] Fix whitespace. --- Modules/_decimal/tests/bench.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/_decimal/tests/bench.py b/Modules/_decimal/tests/bench.py index fef7a220196..23b80b4bedf 100644 --- a/Modules/_decimal/tests/bench.py +++ b/Modules/_decimal/tests/bench.py @@ -78,7 +78,7 @@ for prec in [9, 19]: for func in to_benchmark: start = time.time() if C is not None: - C.getcontext().prec = prec + C.getcontext().prec = prec P.getcontext().prec = prec for i in range(10000): x = func()