慢速正则代码 [英] Slow Regex Code

查看:67
本文介绍了慢速正则代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

还在学习C ++。我正在使用boost写一些正则表达式。它工作得很好。

唯一的事情是......与平等的Perl

和Python相比,这段代码对我来说似乎很慢。我确定我做错了什么。任何提示?


#include< boost / regex.hpp>

#include< iostream>


// g ++ numbers.cpp -o numbers -I / usr / local / include / boost-1_35

/ usr / local / lib / libboost_regex-gcc41-mt-sa

// g ++ numbers.cpp -o numbers.exe

-Ic:// Boost / include / boost-1_35://Boost/lib/libboost_regex-mgw34-mt-s.lib


void number_search(const std :: string& part)

{


static const boost :: regex Numbers (&\\; \\\\\ {9} \\b");

static const boost :: regex& rNumbers =数字;

boost :: smatch匹配;


std :: string :: const_iterator Start = part.begin();

std :: string :: const_iterator End = part.end();


while(boost :: regex_search(Start,End,matches,rNumbers))

{

std :: cout<< matches.str()<< std :: endl;

开始=匹配[0]。秒;

}

}


int main()

{

std :: string部分;

while(std :: getline(std :: cin,part) ))

{

number_search(部分);

}

返回0;

}

解决方案

6月8日下午6:32,brad< byte8b ... @ gmail.comwrote:
< blockquote class =post_quotes>
还在学习C ++。我正在使用boost写一些正则表达式。它

效果很好。唯一的事情是...这个代码对我来说似乎很慢

与Equivelent Perl和Python相比。



似乎很慢,或者速度慢得多。有两个

的可能性:


1.它看起来似乎更慢,因为代码的其余部分显着更快,或者


2.它确实比较慢,因为perl和python可以将它编译成某种高效的字节码,因为它们已经

有一个执行加载这样的字节码的机器。


注意纯(非扩展)正则表达式可以更快地运行,因为它们可以被转换一个纯粹的

DFA。我自己的正则表达式类做到这一点。然而,对于大多数

来说,提升:正则表达式将足够快,并且值得增加灵活性。 (我自己的正则表达式类是

专为特定用途而设计。它不需要

扩展名,但它确实需要一些额外的功能

不在Boost中。对于大多数一般用途,boost :: regex最好是
。)


-

James Kanze(GABI软件)电子邮件:ja ********* @ gmail.com

Conseils eninformatiqueorientéeobjet/

Beratung in objektorientierter Datenverarbeitung

9placeSémard,78210 St.-Cyr-l''coco,France,+ 33(0)1 30 23 00 34

brad写道:


// g ++ numbers.cpp -o numbers -I / usr / local / include / boost-1_35

/ usr / local / lib / libboost_regex-gcc41-mt-sa

// g ++ numbers.cpp -o numbers.exe

-Ic:// Boost / include / boost-1_35://Boost/lib/libboost_regex-mgw34-mt-s.lib



对于初学者,你可以尝试添加一些优化离子标志,例如

-O3和-march =<你的架构(例如。 -march = pentium4)。


(不,我不知道这是否会使正则表达式匹配更快,但是它

不会尝试伤害。)


On Sun,2008年6月8日12:32:30 -0400,brad< by ******* @ gmail.comwrote:


>我正在使用boost编写一些正则表达式。它的效果很好。
唯一的事情就是......与平等的Perl
和Python相比,这段代码对我来说似乎很慢。我确定我做错了什么。有小费吗?



试试PCRE。


-

Roland Pibinger

最好的软件简单,优雅,充满戏剧性。 - Grady Booch


Still learning C++. I''m writing some regex using boost. It works great.
Only thing is... this code seems slow to me compared to equivelent Perl
and Python. I''m sure I''m doing something incorrect. Any tips?

#include <boost/regex.hpp>
#include <iostream>

// g++ numbers.cpp -o numbers -I/usr/local/include/boost-1_35
/usr/local/lib/libboost_regex-gcc41-mt-s.a
// g++ numbers.cpp -o numbers.exe
-Ic://Boost/include/boost-1_35://Boost/lib/libboost_regex-mgw34-mt-s.lib

void number_search(const std::string& portion)
{

static const boost::regex Numbers("\\b\\d{9}\\b");
static const boost::regex& rNumbers = Numbers;
boost::smatch matches;

std::string::const_iterator Start = portion.begin();
std::string::const_iterator End = portion.end();

while (boost::regex_search(Start, End, matches, rNumbers))
{
std::cout << matches.str() << std::endl;
Start = matches[0].second;
}
}

int main ()
{
std::string portion;
while (std::getline(std::cin, portion))
{
number_search(portion);
}
return 0;
}

解决方案

On Jun 8, 6:32 pm, brad <byte8b...@gmail.comwrote:

Still learning C++. I''m writing some regex using boost. It
works great. Only thing is... this code seems slow to me
compared to equivelent Perl and Python.

Seems slow, or is measurably slower. There are two
possibilities:

1. it only seems slower, because the rest of the code is
significantly faster, or

2. it really is slower, because perl and python can compile it
into some sort of efficient byte code, since they already
have an "execution" machine for such byte code loaded.

Note that pure (non-extended) regular expressions can be made to
run considerably faster, since they can be converted to a pure
DFA. My own regular expression class does this. For most
purposes, however, boost:regex will be fast enough, and worth
the added flexibility. (My own regular expression class was
designed for a very specific use. Where it doesn''t need the
extensions, but it does need some additional features which
aren''t in Boost. For most general use, boost::regex is
preferable.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l''école, France, +33 (0)1 30 23 00 34


brad wrote:

// g++ numbers.cpp -o numbers -I/usr/local/include/boost-1_35
/usr/local/lib/libboost_regex-gcc41-mt-s.a
// g++ numbers.cpp -o numbers.exe
-Ic://Boost/include/boost-1_35://Boost/lib/libboost_regex-mgw34-mt-s.lib

For starters, you could try adding some optimization flags, such as
-O3 and -march=<your architecture(eg. -march=pentium4).

(No, I don''t know if that will make the regexp matching faster, but it
doesn''t hurt to try.)


On Sun, 08 Jun 2008 12:32:30 -0400, brad <by*******@gmail.comwrote:

>I''m writing some regex using boost. It works great.
Only thing is... this code seems slow to me compared to equivelent Perl
and Python. I''m sure I''m doing something incorrect. Any tips?

Try PCRE.

--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch


这篇关于慢速正则代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆