字符类与“速记”类的表现(Performance of character classes vs “shorthand” classes)
在回答另一个问题时,提出在显式字符类(
[0-9]
)和“速记”类(\d
)之间可能存在性能差异。 我最初的反应是,如果存在差异,它可以忽略不计,但我没有(并且找不到)任何关于它的信息或者弄清楚我如何测试它。同样,
[^0-9]
,[^\d]
和\D
之间是否存在不可忽略的差异?While answering another question, it was brought up that there might a difference, performance-wise, between an explicit character class (
[0-9]
) and a "shorthand" class (\d
). My initial reaction was that if a difference exists at all, it'd be negligible, but I don't have (and couldn't find) any info about it or figure out how I could test this.Similarly, would there be a non-negligible difference between
[^0-9]
,[^\d]
and\D
?
原文:https://stackoverflow.com/questions/3516545
最满意答案
当有疑问时,BENCHMARK!
我在Perl中对一个简单的正则表达式比较进行了基准测试。 我发现
\d+
确实更快。 因人而异。use strict; use warnings; use Benchmark qw(:all); use feature "switch"; my $r1='\d+'; my $r2='[0-9]+'; my $r3='[[:digit:]]+'; sub test { my @lines = <DATA>; $_=shift; my $RegEx=(caller(0))[3]; given($_) { when(1) { $RegEx=$r1; } when(2) { $RegEx=$r2; } when(3) { $RegEx=$r3; } default { die "$RegEx can't deal with $_\n"; } } my $ln; my $total; my @numbers; foreach my $line (@lines) { $total=0; @numbers=$line=~/($RegEx)/g; $total+=$_ for (@numbers) ; $ln=$numbers[$#numbers]; $total-=$ln; if ($ln != $total) { print "Bad RegEx result: Last Num != total in line!\n"; print "Total=$total, ln=$ln, line:$line"; die; } } } cmpthese(-10, {$r1=>'test(1)', $r2=>'test(2)', $r3=>'test(3)'}); __DATA__ Clip clap clock 1 mouse ran up the clock with 3 hands. The total here is 4. The mouse with 2 ears followed. The total here is 2. After that, the 6 wiskered mouse did dances with 14 second timing. 20. It is hard to make up 5 lines with 2 or 3 numbers in each line. 10. You start thinking about nurserey rhymes with 1 o 2 or 3 number. 6. 1 12 13 123 23 13 55 66 21 45 1 373
我在OS X上使用Perl 5.10 64位得到以下结果:
Rate [[:digit:]]+ [0-9]+ \d+ [[:digit:]]+ 200781/s -- -1% -2% [0-9]+ 202831/s 1% -- -1% \d+ 204605/s 2% 1% --
以及使用Perl 5.10.1的Ubuntu 10.04上的以下内容:
Rate [[:digit:]]+ [0-9]+ \d+ [[:digit:]]+ 264412/s -- -3% -6% [0-9]+ 273202/s 3% -- -3% \d+ 280541/s 6% 3% --
When in doubt, BENCHMARK!
I benchmarked a simple regex comparison in Perl. I found that
\d+
is indeed faster. YMMV.use strict; use warnings; use Benchmark qw(:all); use feature "switch"; my $r1='\d+'; my $r2='[0-9]+'; my $r3='[[:digit:]]+'; sub test { my @lines = <DATA>; $_=shift; my $RegEx=(caller(0))[3]; given($_) { when(1) { $RegEx=$r1; } when(2) { $RegEx=$r2; } when(3) { $RegEx=$r3; } default { die "$RegEx can't deal with $_\n"; } } my $ln; my $total; my @numbers; foreach my $line (@lines) { $total=0; @numbers=$line=~/($RegEx)/g; $total+=$_ for (@numbers) ; $ln=$numbers[$#numbers]; $total-=$ln; if ($ln != $total) { print "Bad RegEx result: Last Num != total in line!\n"; print "Total=$total, ln=$ln, line:$line"; die; } } } cmpthese(-10, {$r1=>'test(1)', $r2=>'test(2)', $r3=>'test(3)'}); __DATA__ Clip clap clock 1 mouse ran up the clock with 3 hands. The total here is 4. The mouse with 2 ears followed. The total here is 2. After that, the 6 wiskered mouse did dances with 14 second timing. 20. It is hard to make up 5 lines with 2 or 3 numbers in each line. 10. You start thinking about nurserey rhymes with 1 o 2 or 3 number. 6. 1 12 13 123 23 13 55 66 21 45 1 373
I get the following results on OS X with Perl 5.10 64 bit:
Rate [[:digit:]]+ [0-9]+ \d+ [[:digit:]]+ 200781/s -- -1% -2% [0-9]+ 202831/s 1% -- -1% \d+ 204605/s 2% 1% --
And the following on Ubuntu 10.04 with Perl 5.10.1:
Rate [[:digit:]]+ [0-9]+ \d+ [[:digit:]]+ 264412/s -- -3% -6% [0-9]+ 273202/s 3% -- -3% \d+ 280541/s 6% 3% --
相关问答
更多-
这是一个非常有趣的问题 - 答案也可能会有趣^^ 考虑事情的最简单的方法可能是: 使用一个instanciated类,其中每个对象都有自己的数据(如用户有一个名字) 使用一个静态类,当它只是一个工具在其他东西(例如,一个语法转换器的BB代码到HTML的工具;它没有自己的生活) (是的,我承认,真的过分简化了...) 关于静态方法/类的一件事是它们不利于单元测试(至少在PHP中,但也可能是其他语言)。 关于静态数据的另外一件事就是程序中只有一个实例存在于你的程序中:如果你将MyClass :: $ myDat ...
-
一个字符类定义了一组字符。 说 - “匹配班级指定的一个字符”。 [:digit:]是一个POSIX字符类, [ ... ]是一个括号表达式。 POSIX类表示法只在括号表达式内有效。 例如, [:digit:]不在括号内的表达式中时,不会被读为POSIX命名类。 相反,在大多数口味中,字面类型包含字符( : , d , i , g , t )。 A character class defines a set of characters. Saying — "match one character spec ...
-
旧方式: .a.b, .a.c, .a.d, .a.e, .a.f 建议的方式(目前很少有浏览器支持) .a:matches(.b, .c, .d, .e, .f) 有些浏览器将其实现为 .a:moz-any(.b, .c, .d, .e, .f) .a:webkit-any(.b, .c, .d, .e, .f) 请参阅MDN - :any伪类 The old way: .a.b, .a.c, .a.d, .a.e, .a.f The proposed way (that few current ...
-
这个词很模糊,似乎没有在任何地方定义,所以很好的问题。 我最了解它意味着像FileInputStream , FileOutputStream , ByteArrayOutputStream等等。为您包装了特定类型的流并提供使用它所需的功能的类。 请注意,大多数这些流都使用character而不是byte ,这通常是您在Java中处理文件中的String数据所需要的。 另一方面,如果您正在读取纯二进制源,那么数据将以byte出现,然后您可以使用InputStreamReader将这些byte转换为chara ...
-
简短的回答是: [class*=gtlab]:not([class*=-16]):not([class*=-15]) 但是,根据您的其余代码和预期的浏览器支持(IE8?),这可能无效。 很长的答案是,如果你有这个选项就改变你的HTML,或者只是使用长版本,它在编码时间或下载时间方面实际上不会花费你更多,并且可能会更快地渲染。 Short answer is: [class*=gtlab]:not([class*=-16]):not([class*=-15]) But depending on the r ...
-
也许你应该考虑使用一些像LESS这样的动态样式表语言。 它允许您创建嵌套类。 例: .top { .normal { font-weight: 400; } .other { font-weight: 700; } } 将编译为: .top .normal {font-weight: 400;} .top .other {font-weight: 700;} Maybe you should consider using some of dynamic styleshe ...
-
目前这是不可能的(使用选择器3推荐)。 CSS没有完整的正则表达式语法,也没有办法压缩多个选择器列表的部分内容。 选择器4提出了一个新的:matches()伪类基于Mozilla的原始:any()实现(请参阅此答案了解一些历史记录): .foo:matches(.a, .b, .c, .d) 当然,不要指望浏览器支持这个。 当那个时间到来时我甚至会忘记更新这个答案,但是......我们会看到。 It's not possible, currently (with the Selectors 3 recom ...
-
您的问题可以通过状态机真正解决,但由于您有一个庞大的类决定该怎么做,似乎您在实现状态模式时遇到了麻烦。 这篇博文有关于在Unity应用程序中实现状态模式的很好解释,您可以轻松地谷歌获取更多资源。 一旦我想创建一个使用方法作为状态的简单状态机,只是为了方便(因为模式使用类作为状态)。 可以使用它作为状态机状态(也可以通过常规状态模式实现)的一种方式,所以如果你想知道这个,你可以读取我的堆栈溢出问题 。 Your problem can really be solved with a state machine ...
-
当有疑问时,BENCHMARK! 我在Perl中对一个简单的正则表达式比较进行了基准测试。 我发现\d+确实更快。 因人而异。 use strict; use warnings; use Benchmark qw(:all); use feature "switch"; my $r1='\d+'; my $r2='[0-9]+'; my $r3='[[:digit:]]+'; sub test { my @lines = ; $_=shift; my $RegEx=( ...
-
让awk使用字符类(Make awk use character classes)[2021-08-29]
你可以尝试使用拼写出来的字符类: [ghoti@pc ~]$ printf "a\n1\nb\n2\nc\n" | awk '/[[:digit:]]/' 1 2 [ghoti@pc ~]$ 就我所知,像\d这样的符号实际上并不是ERE的一部分,它是大多数awk变体(以及The One True Awk )所理解的正则表达式方言。 更新 : 正如在评论中指出的那样,某些Linux发行版可能会安装mawk ,伪装成awk 。 mawk与awk不同。 它是一个最小功能的awk克隆,专为执行速度而非功能而设计 ...