Also, your "||" version isn't very fair, since the /est/ always matches, meaning the /\d/ test is never made. But it is interesting how much faster the "constant" match for est is as opposed to the match for est|\d.
Oh, and use Benchmark:
Results (with 5.6.1, I'm sure 5.8.0 would be somewhat different):#!/usr/bin/perl -w use strict; use Benchmark qw(cmpthese); my $i=0; sub capture { "test$i"=~/(est|\d)/; } sub noCapture { "test$i"=~/(?:est|\d)/; } sub noParens { "test$i"=~/est|\d/; } sub twoRegexen { "test$i"=~/est/ || "test$i"=~/\d/; } cmpthese(-3, { capture => \&capture, noCapture => \&noCapture, noParens => \&noParens, twoRegexen => \&twoRegexen, } );
$ perl testRegexen.pl Benchmark: running capture, noCapture, noParens, twoRegexen, each for +at least 3 CPU seconds... capture: 4 wallclock secs ( 3.18 usr + 0.00 sys = 3.18 CPU) @ 72 +8822.24/s (n=2320570) noCapture: 2 wallclock secs ( 3.10 usr + 0.01 sys = 3.11 CPU) @ 82 +7692.26/s (n=2576606) noParens: 4 wallclock secs ( 3.14 usr + 0.01 sys = 3.15 CPU) @ 87 +1936.55/s (n=2748344) twoRegexen: 4 wallclock secs ( 3.41 usr + -0.00 sys = 3.41 CPU) @ 14 +78048.90/s (n=5047537) Rate capture noCapture noParens twoRegexen capture 728822/s -- -12% -16% -51% noCapture 827692/s 14% -- -5% -44% noParens 871937/s 20% 5% -- -41% twoRegexen 1478049/s 103% 79% 70% --
Edit: Oops, when I added the noParens case I forgot to put it into the cmpthese call here, although I'd put it into my test script.
In reply to Re: RE performance
by RMGir
in thread RE performance
by Vennis
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |