使用pyre2(https://github.com/axiak/pyre2)时,我遇到了性能问题(匹配时间)。
我有三个程序:
使用内置re模块的纯Python:https : //gist.github.com/1873402
使用Pyre2的Python:https://gist.github.com/1873402 。(代码的大部分与1号程序相同。除了使用内置re之外,它会将utf-8字符串解码为unicode,使用pyre2时则不需要)
使用re2的C / C ++:https://gist.github.com/1873417
我测量了两个时间:正则表达式预编译时间和匹配时间。
1号程序:1.65s 1.25s
2号程序:0.04s 1.8s
3号程序:0.02s 0.8s
它们都使用相同的正则表达式和输入。(所有正则表达式均受支持re2)
然后,我遵循了有关Cython中性能分析的文档。得到以下结果:
ncalls tottime percall cumtime percall filename:lineno(function)
652884 16.477 0.000 25.349 0.000 re2.pyx:394(_search)
9479 6.059 0.001 41.806 0.004 export_plain.py:60(匹配)
652884 4.243 0.000 33.602 0.000 {搜索're2.Pattern'对象的方法'
652884 4.010 0.000 29.359 0.000 re2.pyx:442(搜索)
652884 3.056 0.000 3.056 0.000 re2.pyx:114(__ init__)
652953 2.145 0.000 2.145 0.000 {实例}
652884 2.002 0.000 2.002 0.000 …