Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Re^2: Counting keys with defined or undefined elements in a hash (behaviour of values())

by Oberon (Monk)
on Jun 07, 2003 at 01:56 UTC ( [id://263902]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Counting keys with defined or undefined elements in a hash (behaviour of values())
in thread Counting keys with defined or undefined elements in a hash

> It does create a new list, but aliases the scalars rather than copy them.

Right, but I thought that using each didn't create a new list. But slapping a quick Benchmark together I see that keys is nearly 3x faster than each, and values is more than twice as fast as keys ... that totally blows my understanding of the process. And here I've been favoring each over keys or values on the grounds that it used less memory and was faster ... man have I been wrong!

  • Comment on Re: Re^2: Counting keys with defined or undefined elements in a hash (behaviour of values())

Replies are listed 'Best First'.
Re^4: Counting keys with defined or undefined elements in a hash (behaviour of values())
by Aristotle (Chancellor) on Jun 07, 2003 at 02:04 UTC

    Yes, each does not create a list. On the other hand, it does copy the value (and create a new scalar for the key, because the keys in hashes are not full blown scalars).

    Which one to use depends, though - if you're dealing with a DBM tied hash, you'd probably not want to use values.. or for some of the half-million-key monsters I'm dealing with at the moment.

    Also, I tend to prefer each when I'm dealing with every pair stored in hash but in no particular order, because it reduces redundancy. I don't need to name the hash in question more than a single time.

    Makeshifts last the longest.

      > Yes, each does not create a list. On the other hand, it does copy the value ...

      Hmmm ... so would it be fair to say that each does use less memory, even though it's slower?

      > ... (and create a new scalar for the key, because the keys in hashes are not full blown scalars).

      Well, actually, in my test script I was doing

      while (my (undef, $v) = each %fred)

      to avoid that one (at least I hope it avoids it!), but apparently copying the values was enough to make it 6x slower, which really surprised me.

        I think that just throws the created scalar away, but doesn't avoid its creation. The function is not only copying the values, it also has to keep track of its iterator, not to mention and requires a function call for every pair in the hash, rather than a single one for all of them. It does use less memory though, yes, that's what I meant when I said it's desirable when dealing with large hashes.

        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://263902]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-04-20 00:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found