2021-01-16 23:15:24 +08:00
|
|
|
import time
|
|
|
|
|
|
|
|
from decimal import Decimal as D
|
|
|
|
|
2021-01-24 19:00:19 +08:00
|
|
|
times = 500000
|
2021-01-16 23:31:30 +08:00
|
|
|
# 尽量不要超过 5000000 否则整体执行时间>10s
|
|
|
|
# 5000000
|
2021-01-16 23:15:24 +08:00
|
|
|
|
2021-01-16 23:31:30 +08:00
|
|
|
a = 1.1
|
|
|
|
b = 2.5
|
2021-01-16 23:15:24 +08:00
|
|
|
|
2021-01-16 23:31:30 +08:00
|
|
|
# 重新创建 decimal 对象
|
2021-01-16 23:15:24 +08:00
|
|
|
a_start_time = time.time()
|
|
|
|
for x in range(0, times, 1):
|
|
|
|
Da = D(str(a))
|
|
|
|
Db = D(str(b))
|
|
|
|
Dc = Da * Db
|
|
|
|
a_stop_time = time.time()
|
|
|
|
|
2021-01-16 23:31:30 +08:00
|
|
|
# 单次创建 decimal 对象
|
2021-01-16 23:15:24 +08:00
|
|
|
b_start_time = time.time()
|
|
|
|
Da = D(str(a))
|
|
|
|
Db = D(str(b))
|
|
|
|
for x in range(0, times, 1):
|
|
|
|
Dc = Da * Db
|
|
|
|
b_stop_time = time.time()
|
|
|
|
|
2021-01-16 23:31:30 +08:00
|
|
|
# 直接计算
|
2021-01-16 23:15:24 +08:00
|
|
|
c_start_time = time.time()
|
|
|
|
for x in range(0, times, 1):
|
|
|
|
Tc = a * b
|
|
|
|
c_stop_time = time.time()
|
|
|
|
|
2021-01-16 23:31:30 +08:00
|
|
|
print('重新创建 decimal 对象所需时间:', a_stop_time - a_start_time)
|
|
|
|
print('单次创建 decimal 对象所需时间:', b_stop_time - b_start_time)
|
|
|
|
print('直接计算所需时间: ', c_stop_time - c_start_time)
|