解析性能和内存使用情况

紫bor

Pyparsing可以很好地处理很小的语法,但是随着语法的增长,性能下降,内存使用率从高到低。

我当前的语法是:

newline = LineEnd ()
minus = Literal ('-')
plus = Literal ('+')
star = Literal ('*')
dash = Literal ('/')
dashdash = Literal ('//')
percent = Literal ('%')
starstar = Literal ('**')
lparen = Literal ('(')
rparen = Literal (')')
dot = Literal ('.')
comma = Literal (',')
eq = Literal ('=')
eqeq = Literal ('==')
lt = Literal ('<')
gt = Literal ('>')
le = Literal ('<=')
ge = Literal ('>=')
not_ = Keyword ('not')
and_ = Keyword ('and')
or_ = Keyword ('or')
ident = Word (alphas)
integer = Word (nums)

expr = Forward ()
parenthized = Group (lparen + expr + rparen)
trailer = (dot + ident)
atom = ident | integer | parenthized
factor = Forward ()
power = atom + ZeroOrMore (trailer) + Optional (starstar + factor)
factor << (ZeroOrMore (minus | plus) + power)
term = ZeroOrMore (factor + (star | dashdash | dash | percent) ) + factor
arith = ZeroOrMore (term + (minus | plus) ) + term
comp = ZeroOrMore (arith + (eqeq | le | ge | lt | gt) ) + arith
boolNot = ZeroOrMore (not_) + comp
boolAnd = ZeroOrMore (boolNot + and_) + boolNot
boolOr = ZeroOrMore (boolAnd + or_) + boolAnd
match = ZeroOrMore (ident + eq) + boolOr
expr << match
statement = expr + newline
program = OneOrMore (statement)

当我解析以下内容

print (program.parseString ('3*(1+2*3*(4+5))\n') )

这需要很长时间:

~/Desktop/m2/pyp$ time python3 slow.py 
['3', '*', ['(', '1', '+', '2', '*', '3', '*', ['(', '4', '+', '5', ')'], ')']]

real    0m27.280s
user    0m25.844s
sys 0m1.364s

并且内存使用率上升到1.7 GiB(sic!)。

我在实施此语法时是否犯了一些严重的错误,或者我该如何使内存使用率保持在可接受的范围内?

保罗·麦格

导入pyparsing后,启用packrat解析以记录解析行为:

ParserElement.enablePackrat()

这将大大提高性能。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章