mirror of https://github.com/python/cpython.git
gh-108346: Fix failed benchmark in decimal (#108353)
Fix benchmark in decimal to work again after the int str conversion limits.
This commit is contained in:
parent
388d91cd47
commit
b39f65a495
|
@ -7,6 +7,8 @@
|
||||||
|
|
||||||
|
|
||||||
import time
|
import time
|
||||||
|
import sys
|
||||||
|
from functools import wraps
|
||||||
from test.support.import_helper import import_fresh_module
|
from test.support.import_helper import import_fresh_module
|
||||||
|
|
||||||
C = import_fresh_module('decimal', fresh=['_decimal'])
|
C = import_fresh_module('decimal', fresh=['_decimal'])
|
||||||
|
@ -64,66 +66,85 @@ def factorial(n, m):
|
||||||
else:
|
else:
|
||||||
return factorial(n, (n+m)//2) * factorial((n+m)//2 + 1, m)
|
return factorial(n, (n+m)//2) * factorial((n+m)//2 + 1, m)
|
||||||
|
|
||||||
|
# Fix failed test cases caused by CVE-2020-10735 patch.
|
||||||
|
# See gh-95778 for details.
|
||||||
|
def increase_int_max_str_digits(maxdigits):
|
||||||
|
def _increase_int_max_str_digits(func, maxdigits=maxdigits):
|
||||||
|
@wraps(func)
|
||||||
|
def wrapper(*args, **kwargs):
|
||||||
|
previous_int_limit = sys.get_int_max_str_digits()
|
||||||
|
sys.set_int_max_str_digits(maxdigits)
|
||||||
|
ans = func(*args, **kwargs)
|
||||||
|
sys.set_int_max_str_digits(previous_int_limit)
|
||||||
|
return ans
|
||||||
|
return wrapper
|
||||||
|
return _increase_int_max_str_digits
|
||||||
|
|
||||||
print("\n# ======================================================================")
|
def test_calc_pi():
|
||||||
print("# Calculating pi, 10000 iterations")
|
print("\n# ======================================================================")
|
||||||
print("# ======================================================================\n")
|
print("# Calculating pi, 10000 iterations")
|
||||||
|
print("# ======================================================================\n")
|
||||||
|
|
||||||
to_benchmark = [pi_float, pi_decimal]
|
to_benchmark = [pi_float, pi_decimal]
|
||||||
if C is not None:
|
if C is not None:
|
||||||
to_benchmark.insert(1, pi_cdecimal)
|
to_benchmark.insert(1, pi_cdecimal)
|
||||||
|
|
||||||
for prec in [9, 19]:
|
for prec in [9, 19]:
|
||||||
print("\nPrecision: %d decimal digits\n" % prec)
|
print("\nPrecision: %d decimal digits\n" % prec)
|
||||||
for func in to_benchmark:
|
for func in to_benchmark:
|
||||||
start = time.time()
|
start = time.time()
|
||||||
if C is not None:
|
if C is not None:
|
||||||
C.getcontext().prec = prec
|
C.getcontext().prec = prec
|
||||||
P.getcontext().prec = prec
|
P.getcontext().prec = prec
|
||||||
for i in range(10000):
|
for i in range(10000):
|
||||||
x = func()
|
x = func()
|
||||||
print("%s:" % func.__name__.replace("pi_", ""))
|
print("%s:" % func.__name__.replace("pi_", ""))
|
||||||
print("result: %s" % str(x))
|
print("result: %s" % str(x))
|
||||||
print("time: %fs\n" % (time.time()-start))
|
print("time: %fs\n" % (time.time()-start))
|
||||||
|
|
||||||
|
@increase_int_max_str_digits(maxdigits=10000000)
|
||||||
print("\n# ======================================================================")
|
def test_factorial():
|
||||||
print("# Factorial")
|
print("\n# ======================================================================")
|
||||||
print("# ======================================================================\n")
|
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
|
|
||||||
|
|
||||||
for n in [100000, 1000000]:
|
|
||||||
|
|
||||||
print("n = %d\n" % n)
|
|
||||||
|
|
||||||
if C is not None:
|
if C is not None:
|
||||||
# C version of decimal
|
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))
|
||||||
|
|
||||||
|
# Python integers
|
||||||
start_calc = time.time()
|
start_calc = time.time()
|
||||||
x = factorial(C.Decimal(n), 0)
|
y = factorial(n, 0)
|
||||||
end_calc = time.time()
|
end_calc = time.time()
|
||||||
start_conv = time.time()
|
start_conv = time.time()
|
||||||
sx = str(x)
|
sy = str(y)
|
||||||
end_conv = time.time()
|
end_conv = time.time()
|
||||||
print("cdecimal:")
|
|
||||||
|
print("int:")
|
||||||
print("calculation time: %fs" % (end_calc-start_calc))
|
print("calculation time: %fs" % (end_calc-start_calc))
|
||||||
print("conversion time: %fs\n" % (end_conv-start_conv))
|
print("conversion time: %fs\n\n" % (end_conv-start_conv))
|
||||||
|
|
||||||
# Python integers
|
if C is not None:
|
||||||
start_calc = time.time()
|
assert(sx == sy)
|
||||||
y = factorial(n, 0)
|
|
||||||
end_calc = time.time()
|
|
||||||
start_conv = time.time()
|
|
||||||
sy = str(y)
|
|
||||||
end_conv = time.time()
|
|
||||||
|
|
||||||
print("int:")
|
if __name__ == "__main__":
|
||||||
print("calculation time: %fs" % (end_calc-start_calc))
|
test_calc_pi()
|
||||||
print("conversion time: %fs\n\n" % (end_conv-start_conv))
|
test_factorial()
|
||||||
|
|
||||||
if C is not None:
|
|
||||||
assert(sx == sy)
|
|
||||||
|
|
Loading…
Reference in New Issue