正则表达式词性能:\w vs [a-zA-Z0-9_] [英] RegEx word performance: \w vs [a-zA-Z0-9_]
问题描述
我想知道 \w
传递的字符列表,是否只是 [a-zA-Z0-9_]
或者它可能涵盖更多字符?
I'd like to know the list of chars that \w
passes, is it just [a-zA-Z0-9_]
or are there more chars that it might cover?
我问这个问题,因为基于 this,\d
是不同的使用 [0-9]
并且效率较低.
I'm asking this question, because based on this, \d
is different with [0-9]
and is less efficient.
\w
与 [a-zA-Z0-9_]
:哪一个在大规模上可能更快?
\w
vs[a-zA-Z0-9_]
: which one might be faster in large scale?
推荐答案
[ 这个答案是特定于 Perl 的.其中的信息可能不适用于 PCRE 或其他标记语言使用的引擎. ]
/\w/aa
(实际相当于 /[a-zA-Z0-9_]/
)通常更快,但并非总是如此.也就是说,差异非常小(每次检查小于 1 纳秒),因此不应该成为问题.将其置于上下文中,调用子程序或启动正则表达式引擎所需的时间要长得多.
/\w/aa
(the actual equivalent of /[a-zA-Z0-9_]/
) is usually faster, but not always. That said, the difference is so minimal (less than 1 nanosecond per check) that it shouldn't be a concern. To put it in to context, it takes far, far longer to call a sub or start the regex engine.
以下内容对此进行了详细介绍.
What follows covers this in detail.
首先,\w
和 [a-zA-Z0-9_]
默认不同.\w
匹配每个字母、数字、标记和连接符标点符号 Unicode 代码点.其中有 119,821 个![1] 确定哪个是最快的非等效代码毫无意义.
First of all, \w
isn't the same as [a-zA-Z0-9_]
by default. \w
matches every
alphabetic, numeric, mark and connector punctuation Unicode Code Point. There are 119,821 of these![1] Determining which is the fastest of non-equivalent code makes no sense.
然而,使用 \w
和 /aa
确保 \w
只匹配 [a-zA-Z0-9_]代码>.这就是我们将用于基准测试的内容.(实际上,我们会同时使用两者.)
However, using \w
with /aa
ensures that \w
only matches [a-zA-Z0-9_]
. So that's what we're going to be using for our benchmarks. (Actually, we'll use both.)
(请注意,每个测试执行 1000 万次检查,因此 10.0/s 的速率实际上意味着每秒 1000 万次检查.)
(Note that each test performs 10 million checks, so a rate of 10.0/s actually means 10.0 million checks per second.)
ASCII-only positive match
Rate [a-zA-Z0-9_] (?u:\w) (?aa:\w)
[a-zA-Z0-9_] 39.1/s -- -26% -36%
(?u:\w) 52.9/s 35% -- -13%
(?aa:\w) 60.9/s 56% 15% --
在 ASCII 字符中找到匹配项时,仅使用 ASCII 的 \w
和 Unicode \w
都优于显式类.
When finding a match in ASCII characters, ASCII-only \w
and Unicode \w
both beat the explicit class.
/\w/aa
在我的机器上是 ( 1/39.1 - 1/60.9 )/10,000,000 = 0.000,000,000,916 s
/\w/aa
is ( 1/39.1 - 1/60.9 ) / 10,000,000 = 0.000,000,000,916 s faster on my machine
ASCII-only negative match
Rate (?u:\w) (?aa:\w) [a-zA-Z0-9_]
(?u:\w) 27.2/s -- -0% -12%
(?aa:\w) 27.2/s 0% -- -12%
[a-zA-Z0-9_] 31.1/s 14% 14% --
当未能在 ASCII 字符中找到匹配项时,显式类优于纯 ASCII \w
.
When failing to find a match in ASCII characters, the explicit class beats ASCII-only \w
.
/[a-zA-Z0-9_]/
在我的机器上是 ( 1/27.2 - 1/31.1 )/10,000,000 = 0.000,000,000,461 秒
/[a-zA-Z0-9_]/
is ( 1/27.2 - 1/31.1 ) / 10,000,000 = 0.000,000,000,461 s faster on my machine
Non-ASCII positive match
Rate (?u:\w) [a-zA-Z0-9_] (?aa:\w)
(?u:\w) 2.97/s -- -100% -100%
[a-zA-Z0-9_] 3349/s 112641% -- -9%
(?aa:\w) 3664/s 123268% 9% --
哇.这个测试似乎遇到了一些优化.也就是说,多次运行测试会产生极其一致的结果.(其他测试也是如此.)
Whoa. This tests appears to be running into some optimization. That said, running the test multiple times yields extremely consistent results. (Same goes for the other tests.)
在非 ASCII 字符中找到匹配项时,仅 ASCII 的 \w
胜过显式类.
When finding a match in non-ASCII characters, ASCII-only \w
beats the explicit class.
/\w/aa
在我的机器上是 ( 1/3349 - 1/3664 )/10,000,000 = 0.000,000,000,002,57 秒
/\w/aa
is ( 1/3349 - 1/3664 ) / 10,000,000 = 0.000,000,000,002,57 s faster on my machine
Non-ASCII negative match
Rate (?u:\w) [a-zA-Z0-9_] (?aa:\w)
(?u:\w) 2.66/s -- -9% -71%
[a-zA-Z0-9_] 2.91/s 10% -- -68%
(?aa:\w) 9.09/s 242% 212% --
当无法在非 ASCII 字符中找到匹配项时,仅 ASCII 的 \w
胜过显式类.
When failing to find a match in non-ASCII characters, ASCII-only \w
beats the explicit class.
/[a-zA-Z0-9_]/
在我的机器上是 ( 1/2.91 - 1/9.09 )/10,000,000 = 0.000,000,002,34 秒
/[a-zA-Z0-9_]/
is ( 1/2.91 - 1/9.09 ) / 10,000,000 = 0.000,000,002,34 s faster on my machine
结论
- 我很惊讶
/\w/aa
和/[a-zA-Z0-9_]/
之间有任何区别. - 在某些情况下,
/\w/aa
更快;在其他情况下,/[a-zA-Z0-9_]/
. /\w/aa
和/[a-zA-Z0-9_]/
之间的差异非常小(小于 1 纳秒).- 差异很小,您不必担心.
- 即使
/\w/aa
和/\w/u
之间的差异也非常小,尽管后者匹配的字符比前者多 4 个数量级.
- I'm surprised there's any difference between
/\w/aa
and/[a-zA-Z0-9_]/
. - In some situation,
/\w/aa
is faster; in others,/[a-zA-Z0-9_]/
. - The difference between
/\w/aa
and/[a-zA-Z0-9_]/
is very minimal (less than 1 nanosecond). - The difference is so minimal that you shouldn't be concerned about it.
- Even the difference between
/\w/aa
and/\w/u
is quite small despite the latter matching 4 orders of magnitude more characters than the former.
use strict;
use warnings;
use feature qw( say );
use Benchmarks qw( cmpthese );
my %pos_tests = (
'(?u:\\w)' => '/^\\w*\\z/u',
'(?aa:\\w)' => '/^\\w*\\z/aa',
'[a-zA-Z0-9_]' => '/^[a-zA-Z0-9_]*\\z/',
);
my %neg_tests = (
'(?u:\\w)' => '/\\w/u',
'(?aa:\\w)' => '/\\w/aa',
'[a-zA-Z0-9_]' => '/[a-zA-Z0-9_]/',
);
$_ = sprintf( 'use strict; use warnings; our $s; for (1..1000) { $s =~ %s }', $_)
for
values(%pos_tests),
values(%neg_tests);
local our $s;
say "ASCII-only positive match";
$s = "J" x 10_000;
cmpthese(-3, \%pos_tests);
say "";
say "ASCII-only negative match";
$s = "!" x 10_000;
cmpthese(-3, \%neg_tests);
say "";
say "Non-ASCII positive match";
$s = "\N{U+0100}" x 10_000;
cmpthese(-3, \%pos_tests);
say "";
say "Non-ASCII negative match";
$s = "\N{U+2660}" x 10_000;
cmpthese(-3, \%neg_tests);
<小时>
- Unicode 版本 11.
这篇关于正则表达式词性能:\w vs [a-zA-Z0-9_]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!