-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperformance_tests.py
More file actions
181 lines (142 loc) · 5.07 KB
/
Copy pathperformance_tests.py
File metadata and controls
181 lines (142 loc) · 5.07 KB
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
import asyncio
import pandas
from random import randint
from multisort import multisort, mscol, cmp_func, reversor
import test_util as util
pc = util.pc
students = [
{'idx': 0, 'name': 'joh', 'grade': None, 'attend': 100},
{'idx': 1, 'name': 'jan', 'grade': 'a', 'attend': 80},
{'idx': 2, 'name': 'dav', 'grade': 'B', 'attend': 85},
{'idx': 3, 'name': 'bob', 'grade': 'C', 'attend': 85},
{'idx': 4, 'name': 'jim', 'grade': 'F', 'attend': 55},
{'idx': 5, 'name': 'joe', 'grade': None, 'attend': 55}
]
ITERATIONS = 10
EXTRA_ROW = 1000
def main():
results = asyncio.get_event_loop().run_until_complete(run_tests())
rrows = []
for result in results:
if isinstance(result, Exception):
raise result
rrows.append(result)
rrows = multisort(rrows, 1)
table = util.quickTT(['test', 's/iter'])
for rrow in rrows:
table.add_row([rrow[0], f"{(rrow[1] / ITERATIONS):.7f}"])
print("Summary for {0} iteration{1} with {2} rows:\n{3}\n".format(
ITERATIONS,
's' if ITERATIONS > 1 else '',
len(students),
table.draw()))
async def run_tests():
global students
# Add an additional number of records for testing
if EXTRA_ROW > 0:
for i in range(1, EXTRA_ROW+1):
students.append({'idx': len(students),
'name': 'rnd',
'grade': 'ABCDEF'[randint(0, 5)],
'attend': randint(0, 100)})
coroutines = [
run_cmp_func(students[:]),
run_multisort(students[:]),
run_multisort_noclean(students[:]),
run_reversor(students[:]),
run_pandas(students[:]),
superfast(students[:]),
superfast_clean(students[:]),
superfast_lambda(students[:]),
]
res = await asyncio.gather(*coroutines, return_exceptions=True)
return res
async def run_cmp_func(rows):
sw = util.StopWatch()
def cmp_student(a, b):
k = 'grade'
va = a[k]
vb = b[k]
if va != vb:
if va is None:
return -1
if vb is None:
return 1
return -1 if va > vb else 1
k = 'attend'
va = a[k]
vb = b[k]
if va != vb:
return -1 if va < vb else 1
return 0
for i in range(0, ITERATIONS):
_ = sorted(rows, key=cmp_func(cmp_student), reverse=True)
return ('cmp_func', sw.elapsed(prec=7))
async def run_multisort(rows):
sw = util.StopWatch()
for i in range(0, ITERATIONS):
def clean_grade(v): return v.lower()
_ = multisort(rows, [
mscol('grade', reverse=True, clean=clean_grade),
mscol('attend', reverse=True),
], reverse=True)
return ('multisort w/ clean', sw.elapsed(prec=7))
async def run_multisort_noclean(rows):
sw = util.StopWatch()
for i in range(0, ITERATIONS):
_ = multisort(rows, [
mscol('grade', reverse=True),
mscol('attend', reverse=True),
], reverse=True)
return ('multisort noclean', sw.elapsed(prec=7))
async def run_reversor(rows):
sw = util.StopWatch()
for i in range(0, ITERATIONS):
_ = sorted(rows, key=lambda o: (
reversor(None if o['grade'] is None else
o['grade'].lower()),
reversor(o['attend'])
), reverse=True)
return ('reversor', sw.elapsed(prec=7))
async def run_pandas(rows):
sw = util.StopWatch()
for i in range(0, ITERATIONS):
df = pandas.DataFrame(rows[:])
df.sort_values(by=['grade', 'attend'],
ascending=[False, False],
na_position='last')
# d_rows_sorted = list(df.T.to_dict().values())
return ('pandas', sw.elapsed(prec=7))
async def superfast(students):
sw = util.StopWatch()
def key_grade(student):
grade = student['grade']
return grade is None, grade
def key_attend(student):
attend = student['attend']
return attend is None, attend
students_sorted = sorted(students, key=key_attend)
students_sorted.sort(key=key_grade, reverse=True)
return ('superfast', sw.elapsed(prec=7))
async def superfast_lambda(students):
sw = util.StopWatch()
students_sorted = sorted(students, key=lambda r: (r['grade'] is None,
r['grade'],))
students_sorted.sort(key=lambda r: (r['attend'] is None, r['attend'],),
reverse=True)
return ('superfast_lambda', sw.elapsed(prec=7))
async def superfast_clean(students):
sw = util.StopWatch()
def key_grade(student):
grade = student['grade']
if grade is None:
return True, None
else:
return False, grade.upper()
def key_attend(student):
return student['attend']
students_sorted = sorted(students, key=key_attend)
students_sorted.sort(key=key_grade, reverse=True)
return ('superfast w/ clean', sw.elapsed(prec=7))
if __name__ == '__main__':
main()