Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^2: hash slice ? No thanks, I'm about to return...

by Tanktalus (Canon)
on Feb 19, 2005 at 16:30 UTC ( [id://432749]=note: print w/replies, xml ) Need Help??


in reply to Re: hash slice ? No thanks, I'm about to return...
in thread hash slice ? No thanks, I'm about to return...

Or just a "%return" - return is optional. Either way, make the return value explicit, rather than relying on a complex operation to do it, whether you make the fact you're returning explicit or not.

(I did some benchmarks a while back which surprised me to find out that "return %hash" was actually slower than "%hash" and falling out the bottom of the function...)

  • Comment on Re^2: hash slice ? No thanks, I'm about to return...

Replies are listed 'Best First'.
Re^3: hash slice ? No thanks, I'm about to return...
by lidden (Curate) on Feb 19, 2005 at 17:11 UTC
    You surprised me with this, but:
    Rate return no ret return 4682554/s -- -30% no ret 6642457/s 42% --
    Is what i got from:
    use Benchmark qw( cmpthese ); my %hash = (1, 2, 4, 6, 43, 32, 5, 6, 6, 43, 3, 54, 5, 1); cmpthese(0, { 'return' => sub { return \%hash }, 'no ret' => sub { \%hash }, });
      Surprising but pointless. Even the "slow" explicit return runs at 4 million iterations per second. Don't waste your time trying to use this as a real optimization.
Re^3: hash slice ? No thanks, I'm about to return...
by Aristotle (Chancellor) on Feb 19, 2005 at 16:59 UTC

    Depends. I often implicitly return the result of an operation that would obviously be useless in void context, as in

    sub as_string { my $self = shift; join '-', $self->date; }

    or the like.

    Makeshifts last the longest.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://432749]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-19 20:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found