#-*- coding: UTF-8 -*- """ Telco Benchmark for measuring the performance of Fraction calculations http://www2.hursley.ibm.com/decimal/telco.html http://www2.hursley.ibm.com/decimal/telcoSpec.html A call type indicator, c, is set from the bottom (least significant) bit of the duration (hence c is 0 or 1). A r, r, is determined from the call type. Those calls with c=0 have a low r: 0.0013; the remainder (‘distance calls’) have a ‘premium’ r: 0.00894. (The rates are, very roughly, in Euros or dollarates per second.) A price, p, for the call is then calculated (p=r*n). A basic tax, b, is calculated: b=p*0.0675 (6.75%), and the total basic tax variable is then incremented (sumB=sumB+b). For distance calls: a distance tax, d, is calculated: d=p*0.0341 (3.41%), and then the total distance tax variable is incremented (sumD=sumD+d). The total price, t, is calculated (t=p+b, and, if a distance call, t=t+d). The total prices variable is incremented (sumT=sumT+t). The total price, t, is converted to a string, s. """ from fractions import Fraction import os from struct import unpack from time import clock as time from compat import xrange def rel_path(*path): return os.path.join(os.path.dirname(__file__), *path) test = False filename = rel_path("data", "telco-bench.b") def run(): rates = list(map(Fraction, ('0.0013', '0.00894'))) basictax = Fraction("0.0675") disttax = Fraction("0.0341") values = [] with open(filename, "rb") as infil: for i in xrange(20000): datum = infil.read(8) if datum == '': break n, = unpack('>Q', datum) values.append(n) start = time() sumT = Fraction() # sum of total prices sumB = Fraction() # sum of basic tax sumD = Fraction() # sum of 'distance' tax for n in values: calltype = n & 1 r = rates[calltype] p = r * n b = p * basictax sumB += b t = p + b if calltype: d = p * disttax sumD += d t += d sumT += t end = time() return end - start def main(n): run() # warmup times = [] for i in xrange(n): times.append(run()) return times if __name__ == "__main__": import optparse import util parser = optparse.OptionParser( usage="%prog [options]", description="Test the performance of the Telco fractions benchmark") util.add_standard_options_to(parser) options, args = parser.parse_args() util.run_benchmark(options, options.num_runs, main)