def bench_telco(loops, filename):
getcontext().rounding = ROUND_DOWN
rates = list(map(Decimal, ('0.0013', '0.00894')))
twodig = Decimal('0.01')
Banker = Context(rounding=ROUND_HALF_EVEN)
basictax = Decimal("0.0675")
disttax = Decimal("0.0341")
with open(filename, "rb") as infil:
data = infil.read()
infil = io.BytesIO(data)
outfil = six.StringIO()
start = perf.perf_counter()
for _ in range(loops):
infil.seek(0)
sumT = Decimal("0") # sum of total prices
sumB = Decimal("0") # sum of basic tax
sumD = Decimal("0") # sum of 'distance' tax
for i in xrange(5000):
datum = infil.read(8)
if datum == '':
break
n, = unpack('>Q', datum)
calltype = n & 1
r = rates[calltype]
p = Banker.quantize(r * n, twodig)
b = p * basictax
b = b.quantize(twodig)
sumB += b
t = p + b
if calltype:
d = p * disttax
d = d.quantize(twodig)
sumD += d
t += d
sumT += t
print(t, file=outfil)
outfil.seek(0)
outfil.truncate()
return perf.perf_counter() - start
评论列表
文章目录