preg_match()和preg_replace()慢吗? [英] Are preg_match() and preg_replace() slow?

查看:91
本文介绍了preg_match()和preg_replace()慢吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用PHP编码了一段时间,我一直在读,由于必须使用preg_matchpreg_replace,这会降低性能,因此只能使用.为什么是这样?在一个文件中使用20 preg_matches而不是使用另一个PHP函数真的很糟糕.

I've been coding in PHP for a while and I keep reading that you should only use preg_match and preg_replace when you have to because it slows down performance. Why is this? Would it really be bad to use 20 preg_matches in one file instead of using another PHP function.

推荐答案

正如Mike Brant在回答中所说:使用任何preg_*函数没有问题, if 是否需要它们.
您想知道在单个文件中进行20个preg_match调用是否是一个好主意,老实说:我要说的太多了.我经常说如果您的问题解决方案在任何给定时间都依赖于3个以上的正则表达式,那么您就是问题的一部分".不过,我偶尔还是因为自己的口头而犯了罪.

As Mike Brant said in his answer: There's nothing wrong with using any of the preg_* functions, if you need them.
You want to know if it's a good idea to have something like 20 preg_match calls in a single file, well, honestly: I'd say that's too many. I've often stated that "if your solution to a problem relies on more than 3 regex's at any given time, you're part of the problem". I have occasionally sinned against my own mantra, though.

如果您使用20个preg_match调用 ,则只需仔细查看实际的正则表达式,就可以将该数字减半.正则表达式,特别是Perl正则表达式,功能强大,非常值得花时间来了解它们.它们为什么变慢的原因仅是因为正则表达式必须被解析,并在某种较低的水平上转换"为相当数量的分支和循环.例如,如果您想将所有小写的a替换为大写的char,则可以使用正则表达式,但是在PHP中,它看起来像这样:

If you are using 20 preg_match calls, chances are you can halve that number simply by having a closer look at the actual regular expressions. Regex's, especially the Perl regex, are incredibly powerful, and are well worth the time to get to know them. The reason why they tend to be slower is simply because the regex has to be parsed, and "translated" to a considerable number of branches and loops at some low level. If, say, you want to replace all lower-case a's with an upper-case char, you could use a regular expression, sure, but in PHP this would look like this:

preg_replace('/a/','A',$string);

看看表达式的第一个参数:这是一个作为参数传递的字符串.该字符串将被解析(解析时,检查定界符,创建匹配字符串,然后迭代该字符串,将每个字符与模式(在本例中为a)进行比较,并 if 子字符串匹配,将其替换.
似乎有点麻烦,尤其是考虑到我们真正想要的就是最后一步(比较子字符串和替换匹配项).

Look at the expression, the first argument: it's a string that is passed as an argument. This string will be parsed (when parsing, the delimiters are checked, a match string is created and then the string is iterated, each char is compared to the pattern (in this case a), and if the substring matches, it's replaced.
Seems like a bit of a hasstle, especially considering that the last step (comparing substrings and replace matches) is all we really want.

$string = str_replace('a','A',$string);

这样做,无需在解析和验证正则表达式时执行其他检查.
不要忘记preg_match还会构造一个匹配数组,构造一个数组也不是免费的.

Does just that, without the additional checks performed when a regular expression is parsed and validated.
Don't forget that preg_match also constructs an array of matches, and constructing an array isn't free either.

简而言之:正则表达式的速度较慢,因为对表达式进行了解析,验证并最终将其转换为一组简单的低级指令.

In short: regex's are slower because the expression is parsed, validated and finally translated into a set of simple, low-level instructions.

请注意,在某些情况下,人们使用explodeimplode进行字符串操作.同样,这也会创建一个数组,该数组又不是免费的.考虑到此后不久,您将内陷于同一阵列中.也许更需要其他选择(在某些情况下,preg_replace 可以更快).
基本上:正则表达式需要额外的处理,而简单的字符串函数则不需要.但是,如果有疑问,只有一种方法可以绝对确定:设置测试脚本...

Note that, in some cases people use explode and implode for string manipulations. This, too, creates an array which is -again- not free. Considering that you're imploding that very same array shortly thereafter. Perhaps another option is more desirable (and in some cases preg_replace can be faster here).
Basically: regex's need additional processing, that simple string functions don't require. But when in doubt, there's only 1 way to be absolutely sure: set up a test script...

这篇关于preg_match()和preg_replace()慢吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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