Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re^6: keys and values order on a hash

by Somni (Friar)
on Jul 11, 2011 at 19:30 UTC ( #913760=note: print w/replies, xml ) Need Help??

in reply to Re^5: keys and values order on a hash
in thread keys and values order on a hash

Then please, implement a benchmark that exhibits the savings you are concerned about. My benchmark was simply to show that it was not a given that 'each' is faster (or better). The reason they are building arrays is to validate each function does the same thing; given they are both doing the same thing in that regard, the only difference is in the raw speed of an 'each' (and push) loop vs. getting a list of values from keys and values.

Indeed, the semantics of 'each' are clearly defined and obvious. The results of those semantics in a large body of code is not. It's essentially an action-at-a-distance effect. Understanding how that became a problem (in the event it does), and how to find what did it, can make for a very difficult bug to track down.

This is essentially the same reason global variables are generally avoided. If you can always avoid the problems with 'each', and do so when others are looking at and maintaining your code, please, let us all know how you accomplish it; it would go a long way to redeeming 'each' in my opinion, at least.

Replies are listed 'Best First'.
Re^7: keys and values order on a hash
by BrowserUk (Patriarch) on Jul 11, 2011 at 20:11 UTC
    Then please, implement a benchmark that exhibits the savings you are concerned about.

    #! perl -slw use strict; use Benchmark qw[ cmpthese ]; our %hash = 'aaaaa' .. 'dzzzz'; cmpthese 1, { a => q[ my @keys = keys %hash; my @vals = values %hash; my $x; $x = "$keys[ $_ ] :: $vals[ $_ ]" for 0 .. $#keys; ], b => q[ my( $k, $v, $x ); $x = "$k :: $v" while ( $k, $v ) = each %hash; ] }; __END__ s/iter a b a 3.06 -- -41% b 1.79 70% --

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2023-12-11 13:28 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (41 votes). Check out past polls.