Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
On the theory that storing the return from wantarray is always faster than computing it between 2 and 4 times, I was going to suggest the following:
sub trim { if (defined my $w = wantarray) { @_ = ( @_ ? @_ : $_ ); for (@_) { s/^\s+//, s/\s+$// if $_ } return $w ? @_ : $_[0]; } else { for (@_ ? @_ : $_) { s/^\s+//, s/\s+$// if $_ } } }
The if $_ is to protect against warnings for undefined values.

Unfortunately, it turns out that I was wrong. Your version is usually faster than mine. The only place I'm faster is my $x = trim();. I'm not sure that case is worth making the code harder to read. (Though, if this were added to something like Scalar::Util, I would code it my way.)

cmpthese( -1, { japhy => sub { $_ = ' asdf '; trim1(); }, dchild => sub { $_ = ' asdf '; trim2(); }, }); Rate dchild japhy dchild 82708/s -- -3% japhy 85163/s 3% -- cmpthese( -1, { japhy => sub { $_ = ' asdf '; my $n = trim1(); }, dchild => sub { $_ = ' asdf '; my $n = trim2(); }, }); Rate japhy dchild japhy 42766/s -- -12% dchild 48873/s 14% -- cmpthese( -1, { japhy => sub { my $x = ' asdf '; my $n = trim1( $x ); }, dchild => sub { my $x = ' asdf '; my $n = trim2( $x ); }, }); Rate dchild japhy dchild 47733/s -- -5% japhy 50243/s 5% -- cmpthese( -1, { japhy => sub { $_ = ' asdf '; my @n = trim1( ); }, dchild => sub { $_ = ' asdf '; my @n = trim2( ); }, }); Rate japhy dchild japhy 39628/s -- -8% dchild 43115/s 9% -- cmpthese( -1, { japhy => sub { my @l = (' asdf '); my @n = trim1(@l); }, dchild => sub { my @l = (' asdf '); my @n = trim2(@l); }, }); Rate dchild japhy dchild 39628/s -- -3% japhy 40959/s 3% --

Being right, does not endow the right to be rude; politeness costs nothing.
Being unknowing, is not the same as being stupid.
Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.


In reply to Re: trim() magic by dragonchild
in thread trim() magic by japhy

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

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

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (10)
    As of 2015-07-03 17:44 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (54 votes), past polls