I'll let others focus on bench marking and foibles of the regex engine. I note though that you do more work than needed. Consider:

use warnings; use strict; my $fileText = <<TEXT; Dear monks, I'm trying to search for several regexes in some long files. To speed +things up, I tried first checking a combined regex, to see if any of them matches + the line. This seems to speed things up, at least when the regexes are just plai +n words. But: If I try input regexes which are anchored ("^word"), then suddenl +y it's much slower! Is there some weirdness with alternations and anchors? Or + did I make some obvious mistake? (I could rewrite ^aaa|^bbb|^ccc as ^(aaa|bbb|ccc), but it might be tha +t only some of the inputs are anchored.) Like so: TEXT my @argList = qw(regexes that ^I); my $comb = join ('|', @argList); print ">>> OP version\n"; open my $infile, "<", \$fileText; while (my $line = <$infile>) { next if $line !~ $comb; for my $target(@argList) { next if $line !~ $target; print "Matched '$target' in: $line"; } } print "\n>>> Grandfather version\n"; open $infile, "<", \$fileText; while (my $line = <$infile>) { print "Matched '$1' in: $line" while $line =~ /($comb)/g; }

Prints:

>>> OP version Matched 'regexes' in: I'm trying to search for several regexes in some + long files. To speed things up, Matched '^I' in: I'm trying to search for several regexes in some long + files. To speed things up, Matched '^I' in: I tried first checking a combined regex, to see if an +y of them matches the line. Matched 'regexes' in: This seems to speed things up, at least when the + regexes are just plain words. Matched 'regexes' in: But: If I try input regexes which are anchored ( +"^word"), then suddenly it's Matched 'that' in: (I could rewrite ^aaa|^bbb|^ccc as ^(aaa|bbb|ccc), +but it might be that only >>> Grandfather version Matched 'I' in: I'm trying to search for several regexes in some long +files. To speed things up, Matched 'regexes' in: I'm trying to search for several regexes in some + long files. To speed things up, Matched 'I' in: I tried first checking a combined regex, to see if any + of them matches the line. Matched 'regexes' in: This seems to speed things up, at least when the + regexes are just plain words. Matched 'regexes' in: But: If I try input regexes which are anchored ( +"^word"), then suddenly it's Matched 'that' in: (I could rewrite ^aaa|^bbb|^ccc as ^(aaa|bbb|ccc), +but it might be that only
Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

In reply to Re: Alternations and anchors by GrandFather
in thread Alternations and anchors by Chuma

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.