perl预编译的正则表达式问题

use*_*033 2 regex perl

我是否真的从以下方面获得了任何好处(而不是仅在两个if语句中将实际正则表达式替换为$ {pcr})?(实际数据集中有更多行,但仅使用DATA.

my $defs = 0;
my $tests = 0;
my $pcr = qr/\s*[\/\\]?\s*/;
while (<DATA>)
{
    $defs   = ($1 ? 0 : 1) if /<(${pcr})definitions/;
    $tests  = ($1 ? 0 : 1) if /<(${pcr})tests/;
    print "defs: $defs\ntests: $tests\n\n";
}

__DATA__
<what>
</what>
<definitions>
<one />
</definitions>
<tests>
<two />
<three />
</tests>
Run Code Online (Sandbox Code Playgroud)

Ada*_*ire 5

针对您的原始示例运行一些基准测试,没有PCR的示例,以及另一个使用两个不同的PCR definitions并且tests在循环外定义的示例,我在我的机器上获得了以下50万次迭代的结果:

               Rate     no_pcr       orig pcr_before
no_pcr     130208/s         --        -1%        -5%
orig       131579/s         1%         --        -4%
pcr_before 137741/s         6%         5%         --
Run Code Online (Sandbox Code Playgroud)

所以似乎没有任何好处,或者利益非常小.