6
6
import os
7
7
from time import time
8
8
9
- import _peg_parser
10
-
11
9
try :
12
10
import memory_profiler
13
11
except ModuleNotFoundError :
14
- print ("Please run `make venv` to create a virtual environment and install"
15
- " all the dependencies, before running this script." )
12
+ print (
13
+ "Please run `make venv` to create a virtual environment and install"
14
+ " all the dependencies, before running this script."
15
+ )
16
16
sys .exit (1 )
17
17
18
18
sys .path .insert (0 , os .getcwd ())
21
21
argparser = argparse .ArgumentParser (
22
22
prog = "benchmark" , description = "Reproduce the various pegen benchmarks"
23
23
)
24
- argparser .add_argument (
25
- "--parser" ,
26
- action = "store" ,
27
- choices = ["new" , "old" ],
28
- default = "pegen" ,
29
- help = "Which parser to benchmark (default is pegen)" ,
30
- )
31
24
argparser .add_argument (
32
25
"--target" ,
33
26
action = "store" ,
40
33
command_compile = subcommands .add_parser (
41
34
"compile" , help = "Benchmark parsing and compiling to bytecode"
42
35
)
43
- command_parse = subcommands .add_parser (
44
- "parse" , help = "Benchmark parsing and generating an ast.AST"
45
- )
46
- command_notree = subcommands .add_parser (
47
- "notree" , help = "Benchmark parsing and dumping the tree"
48
- )
36
+ command_parse = subcommands .add_parser ("parse" , help = "Benchmark parsing and generating an ast.AST" )
49
37
50
38
51
39
def benchmark (func ):
@@ -66,59 +54,37 @@ def wrapper(*args):
66
54
67
55
68
56
@benchmark
69
- def time_compile (source , parser ):
70
- if parser == "old" :
71
- return _peg_parser .compile_string (
72
- source ,
73
- oldparser = True ,
74
- )
75
- else :
76
- return _peg_parser .compile_string (source )
77
-
78
-
79
- @benchmark
80
- def time_parse (source , parser ):
81
- if parser == "old" :
82
- return _peg_parser .parse_string (source , oldparser = True )
83
- else :
84
- return _peg_parser .parse_string (source )
57
+ def time_compile (source ):
58
+ return compile (source , "<string>" , "exec" )
85
59
86
60
87
61
@benchmark
88
- def time_notree (source , parser ):
89
- if parser == "old" :
90
- return _peg_parser .parse_string (source , oldparser = True , ast = False )
91
- else :
92
- return _peg_parser .parse_string (source , ast = False )
62
+ def time_parse (source ):
63
+ return ast .parse (source )
93
64
94
65
95
- def run_benchmark_xxl (subcommand , parser , source ):
66
+ def run_benchmark_xxl (subcommand , source ):
96
67
if subcommand == "compile" :
97
- time_compile (source , parser )
68
+ time_compile (source )
98
69
elif subcommand == "parse" :
99
- time_parse (source , parser )
100
- elif subcommand == "notree" :
101
- time_notree (source , parser )
70
+ time_parse (source )
102
71
103
72
104
- def run_benchmark_stdlib (subcommand , parser ):
105
- modes = {"compile" : 2 , "parse" : 1 , "notree" : 0 }
73
+ def run_benchmark_stdlib (subcommand ):
74
+ modes = {"compile" : 2 , "parse" : 1 }
106
75
for _ in range (3 ):
107
76
parse_directory (
108
77
"../../Lib" ,
109
78
verbose = False ,
110
79
excluded_files = ["*/bad*" , "*/lib2to3/tests/data/*" ,],
111
- tree_arg = 0 ,
112
80
short = True ,
113
81
mode = modes [subcommand ],
114
- oldparser = (parser == "old" ),
115
82
)
116
83
117
84
118
85
def main ():
119
86
args = argparser .parse_args ()
120
87
subcommand = args .subcommand
121
- parser = args .parser
122
88
target = args .target
123
89
124
90
if subcommand is None :
@@ -127,9 +93,9 @@ def main():
127
93
if target == "xxl" :
128
94
with open (os .path .join ("data" , "xxl.py" ), "r" ) as f :
129
95
source = f .read ()
130
- run_benchmark_xxl (subcommand , parser , source )
96
+ run_benchmark_xxl (subcommand , source )
131
97
elif target == "stdlib" :
132
- run_benchmark_stdlib (subcommand , parser )
98
+ run_benchmark_stdlib (subcommand )
133
99
134
100
135
101
if __name__ == "__main__" :
0 commit comments