in reply to Re^2: Get chars between 2 markers using regular expressions
in thread Get chars between 2 markers using regular expressions
You're only matching the regex once, not collecting all instances of the match. The difference gets more pronounced the longer the string becomes:
gives this on my machine:use strict; use warnings; use Benchmark qw(:all); my $string="He0Hello~~He2W~orld~~He0Hello~~He2W~orld~~He0Hello~~He2W~o +rld~~He0Hello~~He2W~orld~~"; my $f; sub invertedCharclass { while($string=~m/He\d([^~]+)~~/g){$f=$1} } sub nonGreedy { while($string=~m/He\d(.+?)~~/g){$f=$1} } cmpthese (-10, { '[^~]+' => \&invertedCharclass, '.+?' => \&nonGreedy, } );
Rate .+? [^~]+ .+? 105088/s -- -28% [^~]+ 146676/s 40% --
|
|---|