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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |