This commit is contained in:
沈瑗杰 2021-01-16 23:31:30 +08:00
parent f7d2a06371
commit fe318ffe5e

View File

@ -3,14 +3,14 @@ import time
from decimal import Decimal as D
times = 1000000
times = 5000000
# 尽量不要超过 5000000 否则整体执行时间>10s
# 5000000
a = 1
b = 2
c = 0.1
d = 2.1
a = 1.1
b = 2.5
# 重新创建 decimal 对象
a_start_time = time.time()
for x in range(0, times, 1):
Da = D(str(a))
@ -18,6 +18,7 @@ for x in range(0, times, 1):
Dc = Da * Db
a_stop_time = time.time()
# 单次创建 decimal 对象
b_start_time = time.time()
Da = D(str(a))
Db = D(str(b))
@ -25,18 +26,12 @@ for x in range(0, times, 1):
Dc = Da * Db
b_stop_time = time.time()
# 直接计算
c_start_time = time.time()
for x in range(0, times, 1):
Tc = a * b
c_stop_time = time.time()
def test(times, a, b):
t = times
s = time.time()
while t:
t -= 1
e = time.time()
return e - s
print('重新创建 decimal 对象所需时间:', a_stop_time - a_start_time)
print('单次创建 decimal 对象所需时间:', b_stop_time - b_start_time)
print('直接计算所需时间: ', c_stop_time - c_start_time)