Those with benchmarking experience...

If you really want to know (see 2) then why not do it yourself Benchmark made easy

Is the one line solution faster than the two line solution?

2) Do you REALLY care? Does it REALLY matter? Do you for a second think that this is likely to make any measurable difference to the execution speed of your code? It won't.

The most common similar usage is to simultateously strip leading and trailing whitespace from a string. japhy benchmarked the two line and one line examples somewhere, but due to 2) you can Super Search for that yourself :-)

To do it in one line simply need alternation with | and /g to do all two

$_ = '/////usr///bin///perl////'; s!^/+|/+$!/!g; print $_, $/; # but as you can see this is still broken so # assuming it is dealing with *nix paths # you probably want just $_ = '/////usr///bin///perl////'; s!/+!/!g; print $_, $/; # but as tr/// is faster than s/// this will probably be the winner $_ = '/////usr///bin///perl////'; tr!/!/!s; print $_, $/; __DATA__ /usr///bin///perl/ /usr/bin/perl/ /usr/bin/perl/

PS The answer FWIW is that two s/// lines are marnigally faster than one s/// with alternation, but the last s/// regex example is (probably) faster than either option. tr/// will be faster again as tr/// is faster than s/// all other things being equal. Also /* is slower than /+ is slower than /{2,} becuase we only want to do a sub if we have to and we want to effectively fail fast if we have nothing that requires doing. I'll leave it to you to prove these suppositions.....

cheers

tachyon


In reply to Re: Regex - one and only one / at beginning and end of file path by tachyon
in thread Regex - one and only one / at beginning and end of file path by mikedshelton

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.