Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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.

In reply to Re: Answer: How do I remove whitespace at the beginning or end of my string? by ysth
in thread How do I remove whitespace at the beginning or end of my string? by vroom

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2024-04-25 11:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found