Why s/^\s+|\s*$/g rather than s/^\s+|\s+$/g, s/^\s*|\s*$/g or s/^\s*|\s+$/g?

Er, the quantifier mismatch was a typo. I have corrected the original node.

But its good as you can see the speed advantage of the twosub method. Although I suspect you would see a radically different result if the string were more "normal" for instance the content of a node, with the intention of triming each line.

Also you have to be very careful with benchmarking regexes, really subtle differences in the input string and the pattern can result in wildly different run times due to how the optimiser handles them. For instance if your string/pattern facilitates a single FBM search followed by a match followed by a failing FBM search then its going to be massivley faster than a pattern where a FBM search matches many times, each rejected by the regex engine itself afterwards. FBM is really fast, the regex engine is not. In fact despite the common perception that the regex engine itself is fast Id say its not, rather the reputation comes from using a lot of really tricky optimisations to cut down as much as possible how much the regex engine proper is involved. In other words the perl regex engine is perceived as fast mostly because we do our damndest not to use it when we dont need to.

---
$world=~s/war/peace/g


In reply to Re^4: performance enhancement by demerphq
in thread performance enhancement by alandev

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.