Why are you penalizing the one-regex methods by requiring a separate assignment to $_? You should perhaps do 2 separate benchmarks; one assuming $_ is to be changed, and the other assuming a lexical $f is to be changed.
Update: I'm just saying that benchmarks for performing an operation on $_ won't necessarily carry over to benchmarks for performing an operation on a lexical. In fact, with a lexical, the two-regex method outshines the others even more:
use Benchmark 'cmpthese';
use Regexp::Common 'whitespace';
my $f = ' this is a string with spaces to remove ';
cmpthese(-3, {
'Regexp-Common' => sub { my $g=$f; $g=~s/$RE{ws}{crop}//g; },
'two-s///' => sub { my $g=$f; $g=~s/^\s+//; $g=~s/\s+$//; },
'one-s///' => sub { my $g=$f; $g=~s/^\s+|\s+$//g; },
's-capture' => sub { my $g=$f; $g=~s/^\s*(.*?)\s*$/$1/; },
's-capture2' => sub { my $g=$f; $g=~s/^\s*(\S+(?:\s+\S+)?)?\s*$/$
+1/; },
'm-capture' => sub { my $g=$f; ($g) = $g=~/(\S+(?:\s+\S+)?)/;
+ },
});
shows "two-s" gaining more than the others when working on lexicals.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|