-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmicrobench.py
368 lines (336 loc) · 20 KB
/
microbench.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import datetime
import itertools
import operator
import os
import statistics
import timeit
from collections import defaultdict
from decimal import Decimal
from fractions import Fraction as PyFraction
def rel_path(*path):
return os.path.join(os.path.dirname(__file__), *path)
try:
from quicktions import Fraction as QFraction
except ImportError:
import sys
sys.path.insert(0, rel_path('..', 'src'))
from quicktions import Fraction as QFraction
PyFraction.__name__ = "PyFraction"
class_names, classes = list(zip(*[
('float', float),
('Decimal', Decimal),
('QFraction', QFraction),
('PyFraction', PyFraction),
]))
benchmark_values = [
"123",
"76138945735803",
"123.456789",
"1234e10",
"1234e-10",
"983675298736458927343e10",
"983675298736458927343e-10",
"983675298736458927345e10",
"983675298736458927345e-10",
"983675298736458927343e50",
"983675298736458927343e-50",
"983675298736458927345e60",
"983675298736458927345e-60",
]
benchmark_expressions = [
"a + b + c",
"a - b - c",
"a * b * c",
"a / b / c",
"a * 10 + b * 99 + c",
"a * 10 * b + c",
"a / 10 / b - c",
"a * 32543575324 * b / c",
"(a * 32543575324) / (b * 65767564) * c",
"a ** 3 + b ** 2 + c ** 1",
]
PI_DIGITS = (
"3.141592653589793238462643383279502884197169399375105820974944592307816406286"
"208998628034825342117067982148086513282306647093844609550582231725359408128481"
"117450284102701938521105559644622948954930381964428810975665933446128475648233"
"786783165271201909145648566923460348610454326648213393607260249141273724587006"
"606315588174881520920962829254091715364367892590360011330530548820466521384146"
"951941511609433057270365759591953092186117381932611793105118548074462379962749"
"567351885752724891227938183011949129833673362440656643086021394946395224737190"
"702179860943702770539217176293176752384674818467669405132000568127145263560827"
"785771342757789609173637178721468440901224953430146549585371050792279689258923"
"542019956112129021960864034418159813629774771309960518707211349999998372978049"
"951059731732816096318595024459455346908302642522308253344685035261931188171010"
"003137838752886587533208381420617177669147303598253490428755468731159562863882"
"353787593751957781857780532171226806613001927876611195909216420198938095257201"
"065485863278865936153381827968230301952035301852968995773622599413891249721775"
"283479131515574857242454150695950829533116861727855889075098381754637464939319"
"255060400927701671139009848824012858361603563707660104710181942955596198946767"
"837449448255379774726847104047534646208046684259069491293313677028989152104752"
"162056966024058038150193511253382430035587640247496473263914199272604269922796"
"782354781636009341721641219924586315030286182974555706749838505494588586926995"
"690927210797509302955321165344987202755960236480665499119881834797753566369807"
"426542527862551818417574672890977772793800081647060016145249192173217214772350"
"141441973568548161361157352552133475741849468438523323907394143334547762416862"
"518983569485562099219222184272550254256887671790494601653466804988627232791786"
"085784383827967976681454100953883786360950680064225125205117392984896084128488"
"626945604241965285022210661186306744278622039194945047123713786960956364371917"
"287467764657573962413890865832645995813390478027590099465764078951269468398352"
"595709825822620522489407726719478268482601476990902640136394437455305068203496"
"252451749399651431429809190659250937221696461515709858387410597885959772975498"
"930161753928468138268683868942774155991855925245953959431049972524680845987273"
"644695848653836736222626099124608051243884390451244136549762780797715691435997"
"700129616089441694868555848406353422072225828488648158456028506016842739452267"
"467678895252138522549954666727823986456596116354886230577456498035593634568174"
"324112515076069479451096596094025228879710893145669136867228748940560101503308"
"617928680920874760917824938589009714909675985261365549781893129784821682998948"
"722658804857564014270477555132379641451523746234364542858444795265867821051141"
"354735739523113427166102135969536231442952484937187110145765403590279934403742"
"007310578539062198387447808478489683321445713868751943506430218453191048481005"
"370614680674919278191197939952061419663428754440643745123718192179998391015919"
"561814675142691239748940907186494231961567945208095146550225231603881930142093"
"762137855956638937787083039069792077346722182562599661501421503068038447734549"
"202605414665925201497442850732518666002132434088190710486331734649651453905796"
"268561005508106658796998163574736384052571459102897064140110971206280439039759"
"515677157700420337869936007230558763176359421873125147120532928191826186125867"
"321579198414848829164470609575270695722091756711672291098169091528017350671274"
"858322287183520935396572512108357915136988209144421006751033467110314126711136"
"990865851639831501970165151168517143765761835155650884909989859982387345528331"
"635507647918535893226185489632132933089857064204675259070915481416549859461637"
"180270981994309924488957571282890592323326097299712084433573265489382391193259"
"746366730583604142813883032038249037589852437441702913276561809377344403070746"
"921120191302033038019762110110044929321516084244485963766983895228684783123552"
"658213144957685726243344189303968642624341077322697802807318915441101044682325"
"271620105265227211166039666557309254711055785376346682065310989652691862056476"
"931257058635662018558100729360659876486117910453348850346113657686753249441668"
"039626579787718556084552965412665408530614344431858676975145661406800700237877"
"659134401712749470420562230538994561314071127000407854733269939081454664645880"
"797270826683063432858785698305235808933065757406795457163775254202114955761581"
"400250126228594130216471550979259230990796547376125517656751357517829666454779"
"174501129961489030463994713296210734043751895735961458901938971311179042978285"
"647503203198691514028708085990480109412147221317947647772622414254854540332157"
"185306142288137585043063321751829798662237172159160771669254748738986654949450"
"114654062843366393790039769265672146385306736096571209180763832716641627488880"
"078692560290228472104031721186082041900042296617119637792133757511495950156604"
"963186294726547364252308177036751590673502350728354056704038674351362222477158"
"915049530984448933309634087807693259939780541934144737744184263129860809988868"
"741326047215695162396586457302163159819319516735381297416772947867242292465436"
"680098067692823828068996400482435403701416314965897940924323789690706977942236"
"250822168895738379862300159377647165122893578601588161755782973523344604281512"
"627203734314653197777416031990665541876397929334419521541341899485444734567383"
"162499341913181480927777103863877343177207545654532207770921201905166096280490"
"926360197598828161332316663652861932668633606273567630354477628035045077723554"
"710585954870279081435624014517180624643626794561275318134078330336254232783944"
"975382437205835311477119926063813346776879695970309833913077109870408591337464"
"144282277263465947047458784778720192771528073176790770715721344473060570073349"
"243693113835049316312840425121925651798069411352801314701304781643788518529092"
"854520116583934196562134914341595625865865570552690496520985803385072242648293"
"972858478316305777756068887644624824685792603953527734803048029005876075825104"
"747091643961362676044925627420420832085661190625454337213153595845068772460290"
"161876679524061634252257719542916299193064553779914037340432875262888963995879"
"475729174642635745525407909145135711136941091193932519107602082520261879853188"
"770584297259167781314969900901921169717372784768472686084900337702424291651300"
"500516832336435038951702989392233451722013812806965011784408745196012122859937"
"162313017114448464090389064495444006198690754851602632750529834918740786680881"
"833851022833450850486082503930213321971551843063545500766828294930413776552793"
"975175461395398468339363830474611996653858153842056853386218672523340283087112"
"328278921250771262946322956398989893582116745627010218356462201349671518819097"
"303811980049734072396103685406643193950979019069963955245300545058068550195673"
"022921913933918568034490398205955100226353536192041994745538593810234395544959"
"778377902374216172711172364343543947822181852862408514006660443325888569867054"
"315470696574745855033232334210730154594051655379068662733379958511562578432298"
"827372319898757141595781119635833005940873068121602876496286744604774649159950"
"549737425626901049037781986835938146574126804925648798556145372347867330390468"
"838343634655379498641927056387293174872332083760112302991136793862708943879936"
"201629515413371424892830722012690147546684765357616477379467520049075715552781"
"965362132392640616013635815590742202020318727760527721900556148425551879253034"
"351398442532234157623361064250639049750086562710953591946589751413103482276930"
"624743536325691607815478181152843667957061108615331504452127473924544945423682"
"886061340841486377670096120715124914043027253860764823634143346235189757664521"
"641376796903149501910857598442391986291642193994907236234646844117394032659184"
"044378051333894525742399508296591228508555821572503107125701266830240292952522"
"011872676756220415420516184163484756516999811614101002996078386909291603028840"
"026910414079288621507842451670908700069928212066041837180653556725253256753286"
"129104248776182582976515795984703562226293486003415872298053498965022629174878"
"820273420922224533985626476691490556284250391275771028402799806636582548892648"
"802545661017296702664076559042909945681506526530537182941270336931378517860904"
"070866711496558343434769338578171138645587367812301458768712660348913909562009"
"939361031029161615288138437909904231747336394804575931493140529763475748119356"
"709110137751721008031559024853090669203767192203322909433467685142214477379393"
"751703443661991040337511173547191855046449026365512816228824462575916333039107"
"225383742182140883508657391771509682887478265699599574490661758344137522397096"
"834080053559849175417381883999446974867626551658276584835884531427756879002909"
"517028352971634456212964043523117600665101241200659755851276178583829204197484"
"423608007193045761893234922927965019875187212726750798125547095890455635792122"
"103334669749923563025494780249011419521238281530911407907386025152274299581807"
"247162591668545133312394804947079119153267343028244186041426363954800044800267"
"049624820179289647669758318327131425170296923488962766844032326092752496035799"
"646925650493681836090032380929345958897069536534940603402166544375589004563288"
"225054525564056448246515187547119621844396582533754388569094113031509526179378"
"002974120766514793942590298969594699556576121865619673378623625612521632086286"
"922210327488921865436480229678070576561514463204692790682120738837781423356282"
"360896320806822246801224826117718589638140918390367367222088832151375560037279"
"839400415297002878307667094447456013455641725437090697939612257142989467154357"
"846878861444581231459357198492252847160504922124247014121478057345510500801908"
"699603302763478708108175450119307141223390866393833952942578690507643100638351"
"983438934159613185434754649556978103829309716465143840700707360411237359984345"
"225161050702705623526601276484830840761183013052793205427462865403603674532865"
"105706587488225698157936789766974220575059683440869735020141020672358502007245"
"225632651341055924019027421624843914035998953539459094407046912091409387001264"
"560016237428802109276457931065792295524988727584610126483699989225695968815920"
"560010165525637567856672279661988578279484885583439751874454551296563443480396"
"642055798293680435220277098429423253302257634180703947699415979159453006975214"
"829336655566156787364005366656416547321704390352132954352916941459904160875320"
"186837937023488868947915107163785290234529244077365949563051007421087142613497"
"459561513849871375704710178795731042296906667021449863746459528082436944578977"
"233004876476524133907592043401963403911473202338071509522201068256342747164602"
"433544005152126693249341967397704159568375355516673027390074972973635496453328"
"886984406119649616277344951827369558822075735517665158985519098666539354948106"
"887320685990754079234240230092590070173196036225475647894064754834664776041146"
"323390565134330684495397907090302346046147096169688688501408347040546074295869"
"913829668246818571031887906528703665083243197440477185567893482308943106828702"
"722809736248093996270607472645539925399442808113736943388729406307926159599546"
"262462970706259484556903471197299640908941805953439325123623550813494900436427"
"852713831591256898929519642728757394691427253436694153236100453730488198551706"
"594121735246258954873016760029886592578662856124966552353382942878542534048308"
"330701653722856355915253478445981831341129001999205981352205117336585640782648"
"494276441137639386692480311836445369858917544264739988228462184490087776977631"
"279572267265556259628254276531830013407092233436577916012809317940171859859993"
"384923549564005709955856113498025249906698423301735035804408116855265311709957"
"089942732870925848789443646005041089226691783525870785951298344172953519537885"
"534573742608590290817651557803905946408735061232261120093731080485485263572282"
"576820341605048466277504500312620080079980492548534694146977516493270950493463"
"938243222718851597405470214828971117779237612257887347718819682546298126868581"
"705074027255026332904497627789442362167411918626943965067151577958675648239939"
"176042601763387045499017614364120469218237076488783419689686118155815873606293"
"860381017121585527266830082383404656475880405138080163363887421637140643549556"
"186896411228214075330265510042410489678352858829024367090488711819090949453314"
"421828766181031007354770549815968077200947469613436092861484941785017180779306"
"810854690009445899527942439813921350558642219648349151263901280383200109773868"
"066287792397180146134324457264009737425700735921003154150893679300816998053652"
"027600727749674584002836240534603726341655425902760183484030681138185510597970"
"566400750942608788573579603732451414678670368809880609716425849759513806930944"
"940151542222194329130217391253835591503100333032511174915696917450271494331515"
"588540392216409722910112903552181576282328318234254832611191280092825256190205"
"263016391147724733148573910777587442538761174657867116941477642144111126358355"
"387136101102326798775641024682403226483464176636980663785768134920453022408197"
"278564719839630878154322116691224641591177673225326433568614618654522268126887"
"268445968442416107854016768142080885028005414361314623082102594173756238994207"
"571362751674573189189456283525704413354375857534269869947254703165661399199968"
"262824727064133622217892390317608542894373393561889165125042440400895271983787"
"386480584726895462438823437517885201439560057104811949884239060613695734231559"
"079670346149143447886360410318235073650277859089757827273130504889398900992391"
"350337325085598265586708924261242947367019390772713070686917092646254842324074"
"855036608013604668951184009366860954632500214585293095000090715105823626729326"
"453738210493872499669933942468551648326113414611068026744663733437534076429402"
"668297386522093570162638464852851490362932019919968828517183953669134522244470"
"804592396602817156551565666111359823112250628905854914509715755390024393153519"
"090210711945730024388017661503527086260253788179751947806101371500448991721002"
"220133501310601639154158957803711779277522597874289191791552241718958536168059"
"474123419339842021874564925644346239253195313510331147639491199507285843065836"
"193536932969928983791494193940608572486396883690326556436421664425760791471086"
"998431573374964883529276932822076294728238153740996154559879825989109371712621"
"828302584811238901196822142945766758071865380650648702613389282299497257453033"
"283896381843944770779402284359883410035838"
)
def all_types(values, for_cls):
# "123.456" => "'123.456'" -- "123.456" -- "123456, 1000"
if for_cls in (PyFraction, QFraction):
def int_args(a, b):
return f"{a}, {b}"
def types(v):
f = QFraction(v).as_integer_ratio()
return (
repr(v),
v,
int_args(f[0], f[1]),
int_args(f[0]*10, f[1]*100),
int_args(f[0]*99, f[1]*f[0] - 1),
int_args(f[0]*64*64, f[1]*1024*1024),
)
else:
def types(v):
return (
repr(v),
v,
)
return [
tp_v
for v in values
for tp_v in types(v)
]
def run_bm(code, setup='pass', number=200, repeat=1_000, gns=None):
times = timeit.repeat(code, setup, number=number, repeat=repeat, globals=gns)
p10 = len(times) // 10
if p10 >= 1:
times.sort()
times = times[p10:-p10]
return statistics.mean(times) * 1_000_000 # s -> us
def bm_pidigits(pi_string=PI_DIGITS, classes=classes):
gns = {}
for cls in classes:
cls_name = cls.__name__
gns[cls_name] = cls
gns['s'] = pi_string
results = {}
code = f"all({cls_name}(s[:i]) for i in range(3, {len(pi_string)}, 265))"
results[code] = run_bm(code, number=30, repeat=1, gns=gns)
yield (cls, results)
def bm_create(values=benchmark_values, classes=classes):
gns = {}
for cls in classes:
cls_name = cls.__name__
gns[cls_name] = cls
results = {}
for args in all_types(values, cls):
code = f"{cls_name}({args})"
results[code] = run_bm(code, gns=gns)
yield (cls, results)
def bm_calculation(expressions=benchmark_expressions, values=benchmark_values, classes=classes):
gns = {}
for cls in classes:
results = defaultdict(list)
cls_name = cls.__name__
for a, b, c in itertools.product(values, repeat=3):
gns.update(a=cls(a), b=cls(b), c=cls(c))
for expr in expressions:
results[f"{cls_name}: {expr}"].append(run_bm(expr, number=30, repeat=100, gns=gns))
# average over all value combinations
results = {expr: statistics.mean(t) for expr, t in results.items()}
yield (cls, results)
def run_benchmarks(benchmarks=()):
for name, bm in [
('create', bm_create),
('compute', bm_calculation),
('pidigits', bm_pidigits),
]:
if benchmarks and name not in benchmarks:
continue
for cls, result in bm():
yield ((name, cls), result)
def main(*benchmarks):
results_by_type = defaultdict(dict)
now = datetime.datetime.now
start_time = now()
print(start_time)
for key, res in run_benchmarks(benchmarks):
for code, t in sorted(res.items()):
print(f"{code:50s}: {t:8.2f} us")
results_by_type[key].update(res)
print("++", datetime.timedelta(seconds=(now() - start_time).seconds))
avg_by_type = defaultdict(dict)
for (what, cls), results in results_by_type.items():
avg_by_type[what][cls] = statistics.mean(results.values())
for what, timings in sorted(avg_by_type.items(), reverse=True):
print()
print(f"Average times for all '{what}' benchmarks:")
min_avg = min(timings.values())
for cls, avg in sorted(timings.items(), key=operator.itemgetter(1)):
print(f"{cls.__name__:20s}: {avg:8.2f} us ({avg / min_avg:.1f}x)")
if __name__ == '__main__':
import sys
try:
sys.set_int_max_str_digits(len(PI_DIGITS))
except AttributeError:
pass
main(*sys.argv[1:])