-
Notifications
You must be signed in to change notification settings - Fork 1
/
benchmark.py
64 lines (43 loc) · 1.78 KB
/
benchmark.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
from lark import Tree
from lark.visitors import CollapseAmbiguities
import pytest
from lark_ambig_tools import CountTrees, Disambiguator
@pytest.fixture(params=[(2, 2), (6, 2), (2, 6)], ids=["small", "deep", "wide"])
def ambig_tree(request):
data = "abcdefghijklmnopqrstuvwxyz"
def create_tree(depth, ambig):
if depth == 0:
return "A"
if ambig:
tree = Tree("_ambig", [])
else:
tree = Tree(data[depth], [])
for i in range(degree):
tree.children.append(Tree(data[i], []))
for t in tree.children:
for i in range(degree // 2):
if i % 2 == 0:
t.children.append(create_tree(depth - 1, ambig=True))
else:
t.children.append(create_tree(depth - 1, ambig=False))
return tree
depth_factor = request.param[0]
degree = request.param[1]
return create_tree(depth_factor, ambig=True)
def get_all(tree, disambiguator):
list(disambiguator.visit(tree))
def test_disambiguator_all(ambig_tree, benchmark):
benchmark(get_all, ambig_tree, Disambiguator())
def test_disambiguator_counted_all(ambig_tree, benchmark):
counted_tree = CountTrees().transform(ambig_tree)
benchmark(get_all, counted_tree, Disambiguator())
def test_collapse_ambiguities_all(ambig_tree, benchmark):
collapser = CollapseAmbiguities()
benchmark(collapser.transform, ambig_tree)
def get_first(tree, disambiguator):
next(disambiguator.visit(tree))
def test_disambiguator_first(ambig_tree, benchmark):
benchmark(get_first, ambig_tree, Disambiguator())
def test_disambiguator_counted_first(ambig_tree, benchmark):
counted_tree = CountTrees().transform(ambig_tree)
benchmark(get_first, counted_tree, Disambiguator())