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:
- Is there a match?
- Which of the alternations match?
- Where in the string will it match?
- How long is the string?
- How many alternations are there?
- How complicated are the alternations?
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.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.