in reply to Re: Inexplicably slow regex
in thread Inexplicably slow regex

Even with 0.5MB: Still can't confirm!
use Benchmark qw( cmpthese ); our $KEY="df"; { local $/; open my $hosts,'/etc/hosts'; $_= <$hosts>; close $hosts; } $_= $_ x (0.5*1024*1024 / length $_); s/$KEY/XX/g; $_.= "\n df "; print length $_,"\n"; cmpthese(-3, { caret => '/^ \s* $KEY /mx', cr => '/\n \s* $KEY /mx', });


Result

524114
           Rate    cr caret
cr    1108181/s    --   -1%
caret 1118184/s    1%    --

s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
+.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e

Replies are listed 'Best First'.
Re^3: Inexplicably slow regex
by Anonymous Monk on Sep 13, 2006 at 00:22 UTC
    It looks like it needs many lines to have a partial match in order to slow things down. Try executing this line before adding the matching line:
    s/^/ /mg;
      I've put the line just before $_.= "\n    df   "; and still no big difference:
      542450
                 Rate    cr caret
      cr    1105787/s    --   -2%
      caret 1131435/s    2%    --

      s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
      +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
        Thanks for your efforts. I'm at a loss to explain the differences you're seeing. I'm obviously using different input data. It would be interesting to know whether your results differ from the example provided by the user grandfather, since he was able to create a complete self-contained example.