On Thu, Jun 18, 2009 at 07:26:58AM +0200, Michael Monnerie wrote: > On Mittwoch 17 Juni 2009 Theo Van Dinter wrote: > > Yes, it matters (one path is tried then the other has to be tried, as > > opposed to having a single path) > > So which is better performance wise? I guess [sz]? but I'm not sure now.
It's very simple to test, here's a very unscientific one: #!/usr/bin/perl use Benchmark qw(cmpthese); cmpthese(1000000, { 'group_nomatch' => sub { 'abc' =~ /b[xy]?/; }, 'group_match' => sub { 'abc' =~ /b[xc]?/; }, 'alt_nomatch' => sub { 'abc' =~ /b(?:x|y)?/; }, 'alt_match' => sub { 'abc' =~ /b(?:x|c)?/; }, }); Rate alt_match group_nomatch group_match alt_nomatch alt_match 534759/s -- -75% -75% -77% group_nomatch 2127660/s 298% -- -2% -9% group_match 2173913/s 307% 2% -- -7% alt_nomatch 2325581/s 335% 9% 7% -- Not much difference, but alternation having a match ends up very slow. Grouping would logically thinking be better and it seems so. Though it always ends up slightly slower as a not matching alternation? I'm sure there would be other test cases. Where are the perl über-geeks now? ;) Cheers, Henrik