pyparsing性能和内存使用情况

Hyp*_*eus 8 python performance pyparsing

Pyparsing对于非常小的语法工作得很好,但随着语法的增长,性能下降并且通过屋顶使用内存.

我目前的gramar是:

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)
Run Code Online (Sandbox Code Playgroud)

当我解析以下内容时

print (program.parseString ('3*(1+2*3*(4+5))\n') )
Run Code Online (Sandbox Code Playgroud)

这需要很长时间:

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

real    0m27.280s
user    0m25.844s
sys 0m1.364s
Run Code Online (Sandbox Code Playgroud)

内存使用量高达1.7 GiB(原文如此!).

我是否在实现这种语法时犯了一些严重的错误,或者我如何在可忍受的边缘内保留内存使用?

Pau*_*McG 11

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

ParserElement.enablePackrat()
Run Code Online (Sandbox Code Playgroud)

这应该会大大提高性能.

  • 根据记录,在我的计算机上,这从 3.5 秒缩短到 0.036 秒,几乎提高了 100 倍。是否有任何原因导致记忆功能没有自动打开 - 在某些边缘情况下它会失败吗? (2认同)
  • @Hooked:有关使用 pyparsing 进行 Packrat 解析的详细信息,请参阅 [pyparsing FAQ 中的此项](http://pyparsing-public.wikispaces.com/FAQs#toc3)。另请参阅 [this SO thread](/sf/ask/98733421/) 以了解一般的 Packrat 解析。 (2认同)