in reply to Re: Why this code run faster?
in thread Why this code run faster?

/g in scalar context would make every second match fail.
sub f { 'network' =~ /^network$/g } print f()?1:0, "\n" for 1..6;
1 0 1 0 1 0

The failing match should be faster than the succeeding match.

Replies are listed 'Best First'.
Re^3: Why this code run faster?
by gamache (Friar) on Nov 08, 2007 at 17:31 UTC
    Good eyes. When I change the test from if ($phrase2 =~ /^network$/g) {} to while ($phrase2 =~ /^network$/g) {}, the time went from 2.70s to 5.97s. This is in line with what I'd expect -- if 5E6 successful matches and 5E6 failed matches take 2.7s on my CPU, the while loop, consisting of 1E7 successes and 1E7 failures, should take about twice as long.
Re^3: Why this code run faster?
by Gangabass (Vicar) on Nov 09, 2007 at 01:33 UTC

    Thank you guys! You are really help me again.

    And of course special thanks to ikegami.