Skip to content

Commit a9589e1

Browse files
committed
Add average speedup code
1 parent 6fab294 commit a9589e1

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

benchmarks/benchmark_lark_parser.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,16 +151,39 @@ def parse_python(text):
151151
# Data in long-form makes it very easy to use seaborn.
152152
data = pd.DataFrame(rows)
153153
data = data.sort_values(time_key)
154-
print(data)
155154

156155
if RECORD_ALL:
157156
# Show the min / mean if we record all
158157
min_times = data.groupby('key').min().rename({'time': 'min'}, axis=1)
159158
mean_times = data.groupby('key')[['time']].mean().rename({'time': 'mean'}, axis=1)
160159
stats_data = pd.concat([min_times, mean_times], axis=1)
161160
stats_data = stats_data.sort_values('min')
162-
print('Statistics:')
163-
print(stats_data)
161+
else:
162+
stats_data = data
163+
print('Statistics:')
164+
print(stats_data)
165+
166+
if 1:
167+
# Measure speedup
168+
groups = stats_data.groupby('method')
169+
other_keys = sorted(set(stats_data.columns) - {'key', 'method', 'min', 'mean', 'hue_key', 'size_key', 'style_key'})
170+
indexed_groups = {}
171+
for key, group in dict(list(groups)).items():
172+
indexed_group = group.set_index(other_keys)
173+
indexed_groups[key] = indexed_group
174+
cy_data = indexed_groups['parse_cython']
175+
py_data = indexed_groups['parse_python']
176+
speedup_mean = py_data['mean'] / cy_data['mean']
177+
speedup_min = py_data['min'] / cy_data['min']
178+
cy_data['speedup_mean'] = speedup_mean
179+
cy_data['speedup_min'] = speedup_min
180+
average_mean_speedup = cy_data['speedup_mean'].mean()
181+
average_min_speedup = cy_data['speedup_min'].mean()
182+
print('Speedup:')
183+
print(cy_data)
184+
print('Average speedup')
185+
average_speedup = cy_data[['speedup_mean', 'speedup_min']].describe().T
186+
print(average_speedup.drop('count', axis=1))
164187

165188
plot = True
166189
if plot:
@@ -182,7 +205,8 @@ def parse_python(text):
182205
fig.clf()
183206
ax = fig.gca()
184207
sns.lineplot(data=data, x=xlabel, y=time_key, marker='o', ax=ax, **plotkw)
185-
ax.set_title('Benchmark Python Grammar')
208+
209+
ax.set_title(f'Benchmark Grammar: {grammar_fpath.name}\nAverage Speedup: {average_speedup.loc["speedup_mean", "mean"]:0.4f}x')
186210
ax.set_xlabel('Input Size')
187211
ax.set_ylabel('Time (seconds)')
188212
# ax.set_xscale('log')

lark_cython/lark_cython.pyx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
#cython: language_level=3
2+
"""
3+
BASE_DPATH=$(python -c "import lark_cython, pathlib; print(pathlib.Path(lark_cython.__file__).parent)")
4+
cythonize -a -i $BASE_DPATH/lark_cython.pyx
5+
"""
26
import cython
37

48
from copy import copy
@@ -1050,4 +1054,4 @@ plugins = {
10501054
'LexerThread': LexerThread,
10511055
'LALR_Parser': LALR_Parser,
10521056
'_Parser': _Parser, # XXX Ugly
1053-
}
1057+
}

0 commit comments

Comments
 (0)