Skip to content

Instantly share code, notes, and snippets.

@antocuni
Last active April 14, 2017 13:06
Show Gist options
  • Save antocuni/e3d2172c5e9273c0ec0a313b73e74415 to your computer and use it in GitHub Desktop.
Save antocuni/e3d2172c5e9273c0ec0a313b73e74415 to your computer and use it in GitHub Desktop.
pypy telco benchmark
# coding: UTF-8
""" Telco Benchmark for measuring the performance of decimal 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). This is rounded to exactly 2 fractional digits using round-half-even (Banker’s round to nearest).
A basic tax, b, is calculated: b=p*0.0675 (6.75%). This is truncated to exactly 2 fractional digits (round-down), 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%). This is truncated to exactly 2 fractional digits (round-down), 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 __future__ import print_function
from decimal import ROUND_HALF_EVEN, ROUND_DOWN, Decimal, getcontext, Context
import io
import os
from struct import unpack
import perf
import six
from six.moves import xrange
def rel_path(*path):
return os.path.join(os.path.dirname(__file__), *path)
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
if __name__ == "__main__":
import gc
import sys
runner = perf.Runner()
runner.metadata['description'] = "Telco decimal benchmark"
outfile = 'pypy-telco-gc-collect.out'
if os.path.exists(outfile):
print('file exists')
sys.exit(1)
filename = rel_path(".", "telco-bench.b")
#runner.bench_time_func('telco', bench_telco, filename)
values = []
for i in range(250):
if i % 10 == 0:
print(i)
## if i == 100:
## print('disabling the JIT')
## import pypyjit
## pypyjit.set_param('off')
gc.collect()
res = bench_telco(8, filename)
values.append(res)
with open(outfile, 'w') as f:
for v in values:
print(v, file=f)
import sys
import argparse
import matplotlib.pyplot as plt
import perf
import statistics
def parse_file(fname):
values = []
with open(fname) as f:
for line in f:
values.append(float(line))
values = values[1:] # skip the first value
return fname.replace('.out', ''), values
def main():
fnames = sys.argv[1:]
allvalues = [parse_file(fname) for fname in fnames]
for fname, values in allvalues:
x = range(len(values))
plt.plot(x, values, label=fname)
plt.legend(loc='upper right', shadow=True)
plt.show()
if __name__ == "__main__":
main()
0.966940164566
0.242897987366
0.240063905716
0.189076185226
0.203536987305
0.196532011032
0.197757959366
0.190841913223
0.184524059296
0.185909986496
0.186542034149
0.185947179794
0.187633037567
0.190196037292
0.185436964035
0.186137914658
0.187086820602
0.185564994812
0.186209917068
0.186393976212
0.188388109207
0.185793161392
0.187335014343
0.186032056808
0.186288118362
0.186859846115
0.187257051468
0.185904026031
0.18704199791
0.185511112213
0.188170194626
0.185050964355
0.185641050339
0.1849629879
0.186047077179
0.194473981857
0.198625802994
0.186648130417
0.186658143997
0.186042070389
0.187081098557
0.190206050873
0.186682939529
0.184808969498
0.18594121933
0.185257911682
0.186556100845
0.188687086105
0.186136007309
0.186164140701
0.186159133911
0.186806917191
0.186918020248
0.18594789505
0.186069011688
0.185229063034
0.186192989349
0.185468912125
0.186681985855
0.185967922211
0.186020851135
0.185256004333
0.185795068741
0.186175823212
0.186388015747
0.185333013535
0.185992956161
0.18532705307
0.188137054443
0.186640024185
0.186019897461
0.186389923096
0.185965061188
0.186476945877
0.186828136444
0.186779975891
0.186242818832
0.186729192734
0.186597108841
0.187294006348
0.187296867371
0.185735940933
0.186721086502
0.185836076736
0.186658859253
0.186612129211
0.185333013535
0.185786962509
0.186969041824
0.187520980835
0.186872959137
0.184573888779
0.185784816742
0.187546014786
0.188372135162
0.188529014587
0.191001176834
0.193246126175
0.195099115372
0.192377090454
0.194039106369
0.190448999405
0.192049026489
0.188915014267
0.18966293335
0.189316034317
0.192250967026
0.186074018478
0.186303138733
0.186374902725
0.185830116272
0.185063838959
0.185418128967
0.186185836792
0.192671060562
0.185820102692
0.187606811523
0.185260057449
0.186079025269
0.186621904373
0.186429023743
0.18457198143
0.185555934906
0.188060045242
0.188864946365
0.186337947845
0.186433076859
0.187298059464
0.188283920288
0.186170101166
0.188673019409
0.188696861267
0.184877157211
0.186993837357
0.187175989151
0.186102867126
0.18613910675
0.185002803802
0.186819076538
0.185658931732
0.186434984207
0.186803102493
0.185060977936
0.185511112213
0.185294151306
0.185851812363
0.186071872711
0.185870170593
0.184818029404
0.18626499176
0.185935974121
0.186145067215
0.187083005905
0.185214042664
0.185901165009
0.185729026794
0.186218976974
0.186189889908
0.186094045639
0.186450004578
0.186583995819
0.186495065689
0.18621301651
0.184577226639
0.186007976532
0.185348033905
0.187268018723
0.186694145203
0.186247110367
0.186042785645
0.186716079712
0.185559988022
0.187610149384
0.186613082886
0.185662031174
0.186997175217
0.187160015106
0.18655705452
0.186825990677
0.184549093246
0.186041116714
0.187596082687
0.188404083252
0.197831869125
0.192998170853
0.187037944794
0.199149131775
0.1870200634
0.193910837173
0.186633825302
0.187496185303
0.193670988083
0.186608076096
0.191893100739
0.186383962631
0.187180995941
0.193826913834
0.187526941299
0.194120168686
0.185183048248
0.189411878586
0.187341928482
0.18670797348
0.185845851898
0.186860084534
0.185827970505
0.18620800972
0.186785936356
0.188521146774
0.186419010162
0.185645103455
0.185729026794
0.18710899353
0.192328929901
0.191685914993
0.190156936646
0.191391944885
0.19179391861
0.193079948425
0.190771102905
0.191411018372
0.191351890564
0.190359115601
0.186959981918
0.187165021896
0.185961008072
0.186884880066
0.187293052673
0.187155008316
0.187860012054
0.185675859451
0.187760829926
0.189680099487
0.185854911804
0.186751842499
0.185313940048
0.187368869781
0.188179969788
0.186323881149
0.186724901199
0.186129808426
0.187035083771
0.186465024948
0.187175989151
0.187012910843
0.184667110443
0.186682939529
0.186188936234
0.187954902649
0.186563014984
0.975751876831
0.250717163086
0.229788064957
0.203016996384
0.199118852615
0.192268133163
0.196068048477
0.208746194839
0.19385099411
0.193675994873
0.200479984283
0.192191839218
0.189939975739
0.191807031631
0.203356027603
0.198634147644
0.187030792236
0.188210964203
0.197233915329
0.188652038574
0.188750982285
0.188489198685
0.194722890854
0.188457012177
0.189126014709
0.189013004303
0.195631027222
0.187695980072
0.188120126724
0.196900129318
0.189727783203
0.189043998718
0.187431097031
0.195894002914
0.190057992935
0.188565015793
0.188896894455
0.195970773697
0.188789844513
0.188291788101
0.190255880356
0.195582866669
0.18855214119
0.188143014908
0.188102006912
0.200664043427
0.192072868347
0.196597099304
0.188971996307
0.195613145828
0.189936161041
0.188440084457
0.196306943893
0.18776512146
0.188512086868
0.189409971237
0.204082965851
0.192155122757
0.191335916519
0.190625905991
0.209908008575
0.1901679039
0.189660072327
0.188910007477
0.1980509758
0.189515113831
0.190557956696
0.1883289814
0.196851968765
0.18833398819
0.190835952759
0.189896821976
0.190880060196
0.188436985016
0.188392877579
0.201902866364
0.189866065979
0.188154935837
0.195800065994
0.196039915085
0.188448905945
0.188286066055
0.192394971848
0.201652050018
0.196211099625
0.19681596756
0.203896045685
0.194988965988
0.188099145889
0.187457084656
0.188503980637
0.195592880249
0.188350915909
0.188481092453
0.198701858521
0.195021867752
0.192872047424
0.191940069199
0.206231117249
0.198552846909
0.584458827972
0.580801963806
0.589360952377
0.585530042648
0.599718093872
0.587153911591
0.598753929138
0.5907330513
0.610912084579
0.613187074661
0.622815132141
0.607562065125
0.584012985229
0.582159042358
0.582314968109
0.582870006561
0.582440137863
0.593320846558
0.584618091583
0.610867023468
0.602754116058
0.593156814575
0.581864118576
0.598841905594
0.609534978867
0.588964939117
0.581274032593
0.599232912064
0.594789981842
0.594738960266
0.582228899002
0.580570936203
0.597295999527
0.601087808609
0.57986497879
0.58198595047
0.592272996902
0.58404302597
0.582229852676
0.58361697197
0.592837810516
0.586474895477
0.583498001099
0.579560995102
0.585157871246
0.59552192688
0.595151901245
0.594694852829
0.595786094666
0.589991807938
0.587973833084
0.599833011627
0.598613023758
0.609535932541
0.596899986267
0.608840942383
0.589601993561
0.590003967285
0.589418888092
0.595999956131
0.590975999832
0.589850902557
0.642457008362
0.628967046738
0.589115858078
0.586408138275
0.590898990631
0.588186979294
0.588969945908
0.585224866867
0.61896109581
0.592320919037
0.590334892273
0.586019992828
0.59352684021
0.588772058487
0.58905506134
0.581664085388
0.592451095581
0.585675954819
0.600629091263
0.586599111557
0.606979846954
0.602219104767
0.589701890945
0.591946840286
0.58417391777
0.581499099731
0.584593057632
0.588598012924
0.582458972931
0.586536884308
0.583817005157
0.593404054642
0.583488941193
0.583632946014
0.583606958389
0.615941047668
0.614396095276
0.61412191391
0.613950967789
0.607434034348
0.582563877106
0.584390878677
0.591219186783
0.584963083267
0.584871053696
0.581207036972
0.591599941254
0.584333181381
0.583945035934
0.58186006546
0.591369867325
0.584212064743
0.583782911301
0.586489915848
0.592660903931
0.583634138107
0.582701206207
0.585384130478
0.58512711525
0.597732067108
0.586633205414
0.591112852097
0.582262992859
0.583475112915
0.582098960876
0.591641187668
0.583284854889
0.584482908249
0.58185505867
0.591377973557
0.584371089935
0.581918001175
0.586129188538
0.592339992523
0.581986904144
0.584802150726
0.588911056519
0.584673166275
0.581918954849
0.582008123398
0.592045068741
0.581478118896
0.582503795624
0.582206010818
0.589427947998
0.586522102356
0.583567142487
0.581682920456
0.963433980942
0.249760150909
0.231199979782
0.196036815643
0.198004007339
0.191236019135
0.197653055191
0.190712928772
0.189873933792
0.189559221268
0.197468996048
0.191499948502
0.189643859863
0.189678192139
0.200000047684
0.201440811157
0.200335979462
0.199351072311
0.200721025467
0.190598011017
0.197247028351
0.193569898605
0.202600002289
0.190643072128
0.190967082977
0.189469099045
0.197054862976
0.192614793777
0.190956115723
0.198044061661
0.18949508667
0.18986415863
0.190382003784
0.198497056961
0.190370082855
0.193360805511
0.190076828003
0.19759106636
0.193672180176
0.191725969315
0.189527988434
0.19643497467
0.18963599205
0.190975904465
0.190121889114
0.196965932846
0.191247940063
0.192902088165
0.189772844315
0.193181991577
0.189694881439
0.188467979431
0.196935892105
0.189274072647
0.196408033371
0.188482046127
0.197106122971
0.189214944839
0.190248012543
0.190803050995
0.197894096375
0.189306974411
0.188921928406
0.190050125122
0.199590921402
0.190142154694
0.189506053925
0.189778804779
0.20151591301
0.198318958282
0.195209026337
0.192967176437
0.199074029922
0.188174962997
0.19057393074
0.198137998581
0.189446926117
0.19079208374
0.189937829971
0.1980509758
0.190288066864
0.189873933792
0.190305948257
0.196901082993
0.189404010773
0.19367980957
0.190708875656
0.196934223175
0.213772058487
0.191264152527
0.190934181213
0.204195022583
0.200304031372
0.200974941254
0.193603038788
0.191500902176
0.190161943436
0.190370082855
0.197215080261
0.189398050308
0.1916680336
0.189313173294
0.195312976837
0.190634965897
0.190142154694
0.190106868744
0.196667909622
0.190255880356
0.189082145691
0.189778089523
0.195793151855
0.19068312645
0.188457012177
0.189872026443
0.197292089462
0.188500165939
0.192429065704
0.188956975937
0.196281909943
0.19015789032
0.189538955688
0.19852399826
0.191711902618
0.190493822098
0.189394950867
0.197439908981
0.190173149109
0.189432144165
0.189342021942
0.19695687294
0.189287900925
0.190259933472
0.189691066742
0.19627404213
0.189703941345
0.189257144928
0.189028024673
0.195978879929
0.190770864487
0.190191984177
0.189303874969
0.191319942474
0.189426898956
0.187326908112
0.196986913681
0.188717126846
0.189301967621
0.188596963882
0.195647001266
0.188374996185
0.189090967178
0.189629077911
0.20657992363
0.189723014832
0.188488006592
0.18896985054
0.196480035782
0.188355922699
0.188811063766
0.189432144165
0.198338031769
0.18953204155
0.189002037048
0.188640117645
0.19630408287
0.188744068146
0.188750982285
0.198392868042
0.18900513649
0.189596891403
0.190212011337
0.198352098465
0.19007396698
0.189376115799
0.189101934433
0.19811296463
0.189576148987
0.188969135284
0.189607858658
0.199412107468
0.189173936844
0.189348936081
0.189787864685
0.198098897934
0.190742969513
0.189720869064
0.188134908676
0.19175195694
0.189239025116
0.190629005432
0.19727396965
0.188970088959
0.189626932144
0.189316987991
0.19665312767
0.189825057983
0.188575029373
0.188498973846
0.199146986008
0.189730882645
0.18972492218
0.189626932144
0.197044849396
0.191517114639
0.189959049225
0.190300941467
0.202574968338
0.190383195877
0.18998503685
0.188134908676
0.196963071823
0.189378023148
0.189555168152
0.198256015778
0.194442033768
0.197020769119
0.211405992508
0.197634935379
0.190385103226
0.189975976944
0.189383029938
0.196281194687
0.193959951401
0.188251018524
0.188174009323
0.196055173874
0.186960935593
0.187026023865
0.188585996628
0.196630001068
0.188471078873
0.189909934998
0.186851024628
0.19150018692
0.194194793701
0.192373991013
0.20064496994
0.187738895416
0.187317848206
0.187242031097
0.194329023361
0.18665099144
0.187247991562
0.188048124313
0.194794893265
0.187813043594
0.186660051346
0.186079025269
0.195307970047
0.18829703331
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment