为什么这两个正则表达式基准测试差别如此之大?它们使用相同的正则表达式,一个是就地处理,另一个则通过
qr//
存储。
结果:
速率 rege1.FIND_AT_END rege2.FIND_AT_END rege1.FIND_AT_END 661157/s -- -85% rege2.FIND_AT_END 4384042/s 563% -- 速率 rege1.NOFIND rege2.NOFIND rege1.NOFIND 678702/s -- -87% rege2.NOFIND 5117707/s 654% -- 速率 rege1.FIND_AT_START rege2.FIND_AT_START rege1.FIND_AT_START 657765/s -- -85% rege2.FIND_AT_START 4268032/s 549% --
# Benchmark
use Benchmark qw(:all);
my $count = 10000000;
my $re = qr/abc/o;
my %tests = (
"NOFIND " => "cvxcvidgds.sdfpkisd[s"
,"FIND_AT_END " => "cvxcvidgds.sdfpabcd[s"
,"FIND_AT_START " => "abccvidgds.sdfpkisd[s"
);
foreach my $type (keys %tests) {
my $str = $tests{$type};
cmpthese($count, {
"rege1.$type" => sub { my $idx = ($str =~ $re); },
"rege2.$type" => sub { my $idx = ($str =~ /abc/o); }
});
}