If the patterns are simple (patterns with no characters having any special meaning) I expect the loop to be faster. This is because the simpler the string, the more chance there is the optimizer does all, or most, of the work.

For more complicated patterns, it will depend on several things; to name a few:

Whether a loop or a pattern with alternations is faster will depend from case to case. The only way to make sure is to run a benchmark, and even then you only have an answer for a specific set of patterns, a specific set of data, a specific version of Perl (version-number, compilation options, compiler used), and a specific OS. Change one of these things, and you may get a different result.

In reply to Re: Alternation vs. looping for multiple searches. by JavaFan
in thread Alternation vs. looping for multiple searches. by kprice++

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.