use Benchmark qw(cmpthese); cmpthese(-5, { NoPlus => sub { $_ = 'a' . ('x' x 1000) . 'b'; s/x//g } +, Plus => sub { $_ = 'a' . ('x' x 1000) . 'b'; s/x+//g +} });
Produces these results:
$ perl subs.pl Rate NoPlus Plus NoPlus 2674/s -- -98% Plus 115762/s 4230% --
So it would appear that using + for your iteration is 42 times faster than using s///g! Of course the exact difference will depend on the string being matched. When I changed the number of x's to 100 the difference dropped to 12 times. When I changed it to produce a string of 'axaxax...' there was almost no difference between the two solutions.
-sam
In reply to Re: regex internals: quantifiers vs global match
by samtregar
in thread regex internals: quantifiers vs global match
by davidrw
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |