- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
使用 pyre2
( https://github.com/axiak/pyre2 ) 时,我遇到了性能问题(匹配时间)。
我有三个程序:
使用内置 re 模块的纯 Python:https://gist.github.com/1873402
使用 Pyre2 的 Python:https://gist.github.com/1873402 。 (大部分代码与no.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(match) 652884 4.243 0.000 33.602 0.000 {method 'search' of 're2.Pattern' objects} 652884 4.010 0.000 29.359 0.000 re2.pyx:442(search) 652884 3.056 0.000 3.056 0.000 re2.pyx:114(__init__) 652953 2.145 0.000 2.145 0.000 {isinstance} 652884 2.002 0.000 2.002 0.000 re2.pyx:123(__dealloc__) 652953 1.911 0.000 1.911 0.000 re2.pyx:75(unicode_to_bytestring) 652953 1.902 0.000 1.902 0.000 re2.pyx:86(pystring_to_bytestring) 1 0.330 0.330 42.492 42.492 export_plain.py:98(export_fname) 9479 0.173 0.000 0.173 0.000 {built-in method sub} 10000 0.120 0.000 0.120 0.000 {method 'split' of 'str' objects} 8967 0.063 0.000 0.099 0.000 re2.pyx:801(get) 10069 0.061 0.000 0.061 0.000 {method 'strip' of 'str' objects} 69 0.043 0.001 0.146 0.002 re2.pyx:806(prepare_pattern) 9036 0.038 0.000 0.038 0.000 re2.pyx:788(__next) 69 0.022 0.000 0.169 0.002 re2.pyx:905(_compile) 1 0.005 0.005 0.177 0.177 export_plain.py:36(load) 69 0.002 0.000 0.003 0.000 re2.pyx:784(__init__) 69 0.001 0.000 0.170 0.002 re2.pyx:763(compile) 38 0.001 0.000 0.001 0.000 {method 'write' of 'file' objects} 69 0.001 0.000 0.171 0.002 {re2.compile} 1 0.001 0.001 42.669 42.669 export_plain.py:160(main) 3 0.000 0.000 0.000 0.000 {open} 69 0.000 0.000 0.000 0.000 {method 'append' of 'list' objects} 19 0.000 0.000 0.000 0.000 {method 'join' of 'str' objects} 1 0.000 0.000 0.000 0.000 genericpath.py:38(isdir) 1 0.000 0.000 42.669 42.669 export_plain.py:153(run_re2_test) 1 0.000 0.000 0.000 0.000 {posix.stat} 4 0.000 0.000 0.000 0.000 {time.time} 1 0.000 0.000 0.000 0.000 posixpath.py:59(join) 1 0.000 0.000 42.670 42.670 :1() 1 0.000 0.000 0.000 0.000 {method 'encode' of 'unicode' objects} 3 0.000 0.000 0.000 0.000 {method 'rfind' of 'str' objects} 2 0.000 0.000 0.000 0.000 posixpath.py:109(basename) 1 0.000 0.000 0.000 0.000 posixpath.py:117(dirname) 1 0.000 0.000 0.000 0.000 stat.py:40(S_ISDIR) 2 0.000 0.000 0.000 0.000 {len} 1 0.000 0.000 0.000 0.000 {method 'extend' of 'list' objects} 1 0.000 0.000 0.000 0.000 {method 'startswith' of 'str' objects} 1 0.000 0.000 0.000 0.000 {method 'endswith' of 'str' objects} 1 0.000 0.000 0.000 0.000 stat.py:24(S_IFMT) 1 0.000 0.000 0.000 0.000 {method '__enter__' of 'file' objects} 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
看起来 _search
函数 (re2.pyx:393) 占用了太多时间。但我不知道这和纯 C 版本怎么会有这么大的不同。
543f228
re2 修订版:变更集:79:0c439a6bd795
我猜实际的 Match
函数 (re2.pyx:424) 花费了该函数中的大部分时间。
然后,我将 Match 函数重构为 cdef 函数 _my_match
,以便我可以在配置文件结果中看到它,还将 StringPiece
分配重构为 cdef 函数 _alloc_sp
. (修改详细信息: https://gist.github.com/1873993 )重新配置它,然后得到:
Mon Feb 20 20:52:47 2012 Profile.prof 3975043 function calls in 28.265 CPU seconds Ordered by: internal time ncalls tottime percall cumtime percall filename:lineno(function) 652884 10.060 0.000 20.230 0.000 re2.pyx:452(search) 652884 4.131 0.000 28.201 0.000 {method 'search' of 're2.Pattern' objects} 652884 3.647 0.000 3.647 0.000 re2.pyx:394(_my_match) 9479 3.037 0.000 31.238 0.003 export_plain.py:62(match) 652884 2.901 0.000 2.901 0.000 re2.pyx:443(_alloc_sp) 652953 1.814 0.000 1.814 0.000 re2.pyx:86(pystring_to_bytestring) 652953 1.808 0.000 1.808 0.000 re2.pyx:75(unicode_to_bytestring) 1 0.332 0.332 31.926 31.926 export_plain.py:96(export_fname) 9479 0.169 0.000 0.169 0.000 {built-in method sub} 10000 0.122 0.000 0.122 0.000 {method 'split' of 'str' objects} 8967 0.065 0.000 0.099 0.000 re2.pyx:849(get) 10069 0.064 0.000 0.064 0.000 {method 'strip' of 'str' objects} 69 0.042 0.001 0.142 0.002 re2.pyx:854(prepare_pattern) 9036 0.035 0.000 0.035 0.000 re2.pyx:836(__next) 69 0.023 0.000 0.166 0.002 re2.pyx:953(_compile) 1 0.003 0.003 32.103 32.103 export_plain.py:158(main) 1 0.003 0.003 0.174 0.174 export_plain.py:36(load) 69 0.002 0.000 0.168 0.002 re2.pyx:811(compile) 38 0.001 0.000 0.001 0.000 {method 'write' of 'file' objects} 69 0.001 0.000 0.169 0.002 {re2.compile} 69 0.001 0.000 0.001 0.000 re2.pyx:832(__init__) 1 0.001 0.001 32.104 32.104 export_plain.py:151(run_re2_test) 1 0.000 0.000 32.105 32.105 :1() 2 0.000 0.000 0.000 0.000 {len} 3 0.000 0.000 0.000 0.000 {open} 1 0.000 0.000 0.000 0.000 {method 'extend' of 'list' objects} 69 0.000 0.000 0.000 0.000 {isinstance} 69 0.000 0.000 0.000 0.000 {method 'append' of 'list' objects} 19 0.000 0.000 0.000 0.000 {method 'join' of 'str' objects} 4 0.000 0.000 0.000 0.000 {time.time} 1 0.000 0.000 0.000 0.000 {method 'encode' of 'unicode' objects} 1 0.000 0.000 0.000 0.000 posixpath.py:59(join) 1 0.000 0.000 0.000 0.000 {posix.stat} 1 0.000 0.000 0.000 0.000 genericpath.py:38(isdir) 2 0.000 0.000 0.000 0.000 posixpath.py:109(basename) 3 0.000 0.000 0.000 0.000 {method 'rfind' of 'str' objects} 1 0.000 0.000 0.000 0.000 posixpath.py:117(dirname) 1 0.000 0.000 0.000 0.000 stat.py:40(S_ISDIR) 1 0.000 0.000 0.000 0.000 {method 'startswith' of 'str' objects} 1 0.000 0.000 0.000 0.000 {method 'endswith' of 'str' objects} 1 0.000 0.000 0.000 0.000 {method '__enter__' of 'file' objects} 1 0.000 0.000 0.000 0.000 stat.py:24(S_IFMT) 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
但是搜索
仍然占用很多时间(总计 10.060)。
谁能找出问题所在吗?
最佳答案
嗯,这取决于...pyre2 渐近更快,但对于每个特定正则表达式不一定更快。原因是 re2 生成 NFA 并同时在所有事件状态上行走。 re(如果我是对的)一次只尝试 NFA 中的一条路径,如果失败,它会回溯并尝试另一条路径。这意味着 re 可以做各种奇特的事情,比如前瞻等,因为它总是记住与给定字符串匹配的路径,而 re2 只记住当前的事件状态。这意味着 re2 会告诉您字符串是否与正则表达式匹配,但它无法执行您可以使用 re 对组执行的所有奇特计算。因此,pyre2 具有线性渐近时间复杂度(以不支持内置 re 的某些语法为代价),而 re 具有指数渐近复杂度。然而,这并不意味着对于基本的简单正则表达式,pyre2 必须表现得更好。
还有一点要记住:
您是否从facebook repository下载了pyre2?或来自python package index ?如果你从 python 包索引下载,如果它无法处理给定的正则表达式,它将回退到内置的 re 库(所以我想那里可能会有一些小的开销) - 无论如何,如果你匹配的正则表达式是pyre2不支持,它将回退到重新,并且至少不会表现得更好。
因此,如果没有看到您的正则表达式,很难说,但我猜测 re2 速度较慢,原因之一是:
您的正则表达式和与之匹配的字符串都非常简单(在这种情况下,使用 re2 没有任何优势)
或者您从 pypi 下载了pyre2,并且您正在捕获组并使用前瞻,而 re2 不支持它,因此它会回退到 re)
由于您设法在 C re2 库中编译相同的正则表达式,我猜这是第一个原因,而不是第二个。
关于python - Pyre2 比内置 re 模块慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9371790/
自己试试看: import pandas as pd s=pd.Series(xrange(5000000)) %timeit s.loc[[0]] # You need pandas 0.15.1
我最近开始使用 Delphi 中的 DataSnap 来生成 RESTful Web 服务。在遵循 Marco Cantu 本人和互联网上其他几个人的指导后,我成功地使整个“链条”正常工作。 但是有一
我一直在为操作系统类(class)编写以下代码,但结果有些奇怪。该代码创建x线程并同时运行它们,以便将两个平方矩阵相乘。每个线程将输入矩阵的Number_of_rows/Number_of_threa
我正在尝试确定何时使用 parallel包以加快运行某些分析所需的时间。我需要做的一件事是创建矩阵,比较具有不同行数的两个数据框中的变量。我在 StackOverflow 上问了一个关于有效方法的问题
我最近对我的代码进行了一些清理,并在此过程中更改了此内容(不完全是真实的代码): read = act readSTRef test1 term i var = do t v^!terms.
我正在计时查询和同一个查询的执行时间,分页。 foreach (var x in productSource.OrderBy(p => p.AdminDisplayName) .Wher
我正在开发一个项目 (WPF),我有一个 Datagrid 从数据库加载超过 5000 条记录,所以我使用 BackgroundWorker 来通知用户数据正在加载,但它太慢了,我需要等待将近 2分钟
我在查询中添加 ORDER BY 时遇到问题。没有 ORDER BY 查询大约需要 26ms,一旦我添加 ORDER BY,它大约需要 20s。 我尝试了几种不同的方法,但似乎可以减少时间。 尝试 F
我是 Android 开发新手,遇到了性能问题。当我的 GridView 有太多项目时,它会变得有点慢。有什么方法可以让它运行得更快一些吗? 这是我使用的代码: 适配器: public class C
这里的要点是: 1.设置query_cache_type = 0;重置查询缓存; 2.在 heidisql(或任何其他客户端 UI)中运行任何查询 --> 执行,例如 45 毫秒 3.使用以下代码运行
想象下表: CREATE TABLE drops( id BIGSERIAL PRIMARY KEY, loc VARCHAR(5) NOT NULL, tag INT NOT
我的表 test_table 中的示例数据: date symbol value created_time 2010-01-09 symbol1
首先,如果已经有人问过这个问题,我深表歉意,至少我找不到任何东西。 无论如何,我将每 5 分钟运行一次 cron 任务。该脚本加载 79 个外部页面,而每个页面包含大约 200 个我需要在数据库中检查
我有下面的 SQL 代码,它来自 MySQL 数据库。现在它给了我期望的结果,但是查询很慢,我想我应该在进一步之前加快这个查询的速度。 表agentstatusinformation有: PKEY(主
我需要获取一个对象在 Core Data 中数千个其他对象之间的排名。现在,这是我的代码: - (void)rankMethod { //Fetch all objects NSFet
我正在编写一个应用程序,我需要在其中读取用户的地址簿并显示他所有联系人的列表。我正在测试的 iPhone 有大约 100 个联系人,加载联系人确实需要很多时间。 ABAddressBookRef ad
我正在使用 javascript 将 160 行添加到包含 10 列的表格中。如果我这样做: var cellText = document.createTextNode(value); cell.a
我是 Swift 的新手,我已经设置了一个 tableView,它从 JSON 提要中提取数据并将其加载到表中。 表格加载正常,但是当表格中有超过 10 个单元格时,它会变得缓慢且有些滞后,特别是它到
我在 InitializeCulture 和 Page_PreInit 事件之间的 asp.net 页面中遇到性能问题。当我重写 DeterminePostBackMode() 时,我发现问题出在 b
我在 Hetzner 上有一个带有 256GB RAM 6 个 CPU(12 个线程) 的专用服务器,它位于德国。我有 CENTOS 7.5。 EA4。 我的问题是 SSL。每天大约 2 小时,我们在
我是一名优秀的程序员,十分优秀!