-
Notifications
You must be signed in to change notification settings - Fork 1
/
benchmarkGen.py
97 lines (83 loc) · 2.53 KB
/
benchmarkGen.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
#!/usr/bin/env python3
import itertools
from collections import OrderedDict
from keyword import kwlist
from argparse import *
kwset=set(kwlist)
def varsNamesGen(alph, l=255):
i=0
j=1
while True:
for name in map(lambda x: "".join(x), itertools.combinations(alph, j)):
if name not in kwset:
yield name
i+=1
if i>l:
return
j+=1
def genVarNames(len=255):
a=ord("a")
alphabetLen=26
alphabet=[chr(i) for i in range(a, a+alphabetLen)]
a=ord("A")
alphabet.extend([chr(i) for i in range(a, a+alphabetLen)])
return list(varsNamesGen(alphabet, len))
def genFunc(name, varNames, indent=0):
return "\n".join((
"\t"*indent+"def "+name+"():",
"\t"*(indent+1)+"return "+"+".join(varNames),
""
))
def genVarsInitializer(varsDict, indent=0):
return "\n".join((indent*"\t"+"=".join(i) for i in varsDict.items()))+"\n"
def genMeasuredFunc(name, varsDict, indent=0):
return "".join((
genFunc(name, varsDict.keys(), indent),
genVarsInitializer(varsDict, indent),
"\t"*indent+name+"_inlined=bind.bind("+name+", locals())"
))
def genClosureMeasuredFunc(name, varsDict, innerName=None, indent=0):
if innerName is None:
innerName="inner_"+name
return "\n".join((
"\t"*indent+"def "+name+"_gen():",
genMeasuredFunc(innerName, varsDict, indent+1),
"\t"*(indent+1)+"return ("+innerName+", "+innerName+"_inlined)\n",
indent*"\t"+"("+name+", "+name+ "_inlined)="+name+"_gen()",
""
))
def genMeasurement(name):
return "\n".join((
measResultVarName+"['"+name+"']=OrderedDict((",
"\t('orig' , timeit.timeit("+name+" )),",
"\t('inlined', timeit.timeit("+name+"_inlined)),",
"))"
))
def genDis(name):
return "dis.dis("+name+")"
measResultVarName="measResults"
preamble=r"""
import dis, json, timeit, bind
from collections import OrderedDict
def computeDelta(res):
for i in res:
res[i]["% faster"]=(res[i]["orig"]-res[i]["inlined"])/res[i]["orig"]*100
"""+ measResultVarName+"={}"
if __name__ == "__main__":
argp=ArgumentParser(
description="Generates code for benchmarking the library"
)
argp.add_argument("--count", type=int, help="Count of variables per function to be inlined", default=0xff)
args=argp.parse_args()
varNames=genVarNames(args.count)
varsDict=dict(zip(varNames, map(str, range(len(varNames)))))
text="\n".join((
preamble,
genClosureMeasuredFunc("load_deref", varsDict),
genMeasuredFunc("load_global", varsDict),
genMeasurement("load_global"),
genMeasurement("load_deref"),
"computeDelta("+measResultVarName+")",
"print(json.dumps("+measResultVarName+", indent='\t'))"
))
print(text)