...because greediness does not affect its result in any way.
I was hoping someone would have an idea as to what he might have been thinking.
You're only thinking about the final outcome. Sometimes, changes are not made for the outcome, but because it changes how it performs, and one method may be better for the normally expected input.
Unfortunately, in this case, I'm not seeing that on my system the non-greedy being significantly better at matching the short version, and it's significantly worse at matching the long version:
use Benchmark qw(cmpthese); my $f = '-f'; my $f2 = '-field'; cmpthese( 500000, { 'ng_short' => sub { $f =~ /^-f(i(e(ld??)??)??)??$/ }, 'ng_long' => sub { $f2 =~ /^-f(i(e(ld??)??)??)??$/ }, 'g_short' => sub { $f =~ /^-f(i(e(ld?)?)?)?$/ }, 'g_long' => sub { $f2 =~ /^-f(i(e(ld?)?)?)?$/ }, } ); # Rate ng_long g_long g_short ng_short # ng_long 88339/s -- -54% -73% -74% # g_long 193798/s 119% -- -41% -42% # g_short 328947/s 272% 70% -- -2% # ng_short 335570/s 280% 73% 2% --
Oh -- and it _does_ change the values of $1, $2, $3, but we have no reason from the code snippet given that they're important.,/strike>
Update: Removed extra comma, resulting in sloppy english grammer, and stressed the 'not'. Was :
Unfortunately, in this case, I'm not seeing that on my system, the non-greedy being significantly better at matching the short version ...
Update: tye is correct in the lack of difference. Why a non-greedy expression would match 1 when it has the option of matching 0 makes no sense to me, though.
In reply to Re^3: Dominus on the non-greedy version of the ? quantifier
by jhourcle
in thread Dominus on the non-greedy version of the ? quantifier
by Aristotle
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |