Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

( [id://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":



  • 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 sharing their wisdom with the Monastery: (4)
As of 2024-03-29 12:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found