use strict; use warnings; use Benchmark; my @strings = qw(exception:tex exception:mex asdf); Benchmark::cmpthese( -5, { 'one' => sub { my @filtered = grep { /exception:(?!tex)/} @strings +; }, 'two' => sub { my @filtered = grep { /exception/ && !/tex/ } @stri +ngs; }, }); __END__ Rate one two one 175458/s -- -15% two 207611/s 18% --

update: The filter expressions are different. The second one does not care whether tex is before of after exception and doesn't require a ':' after 'exception'. I have stumbled on a patch of my ignorance trying to make the second match the same strings as the first. The best I have so far is:

use strict; use warnings; use Benchmark; my @strings = qw(exception:tex exception:mex asdf tex:exception:mex); Benchmark::cmpthese( -5, { 'one' => sub { my @filtered = grep { /exception:(?!tex)/} @strings +; }, 'two' => sub { my @filtered = grep { /exception/ && !/tex/ } @stri +ngs; }, 'three' => sub { my @filtered = grep { pos = 0; /exception:/g && ! +/\Gtex/ } @strings; }, }); __END__ Rate three one two three 99554/s -- -18% -38% one 121692/s 22% -- -24% two 161067/s 62% 32% --

update: see also strange behavior of grep with global match [resolved]. Adding "pos = 0" is necessary to clear pos between iterations but it would not be necessary if there were only a single iteration. This makes it difficult to compare the efficiency of "/exception/g && !/\Gtex/".


In reply to Re^3: Match with line without a word by ig
in thread Match with line without a word by Heaven

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.