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

Re: map-like hash iterator

by Aristotle (Chancellor)
on Nov 06, 2002 at 20:19 UTC ( [id://210874]=note: print w/replies, xml ) Need Help??


in reply to map-like hash iterator

Or maybe even
sub hashit (&\%) { my ($c, $h) = @_; local ($a, $b); map { ($a, $b) = each %$h; $c->() } (undef) x keys %$h; }

Makeshifts last the longest.

Replies are listed 'Best First'.
Re: Re: map-like hash iterator
by jdporter (Paladin) on Nov 06, 2002 at 20:51 UTC
    Yeah... but if the hash has 10_000_000 keys, it's probably no better to have a list of 10_000_000 undefs than a list of 10_000_000 actual key values.

    Maybe this instead:

    my $n = keys %$h; for ( my $i = 0; $i < $n; $i++ ) { local( $a, $b ) = each %$h; $c->() }

      The idea was to be able to use map to build the return list. Obviously that makes little sense in void context, which is all your for proposition will be able to provide.

      undefs are actually less wasteful than actual values - my @x = (undef) x 10,000,000; consumes 130MB for me, my @x = ('a'x10) x 10,000,000; nearly hits the 500MB mark.

      Of course, if you're not in void context and actually intent on returning the resulting list from processing a 10,000,000 key hash, you'll have to be able to fit that in memory anyway. Since you're throwing around big chunks of data, memory can't be a huge concern, otherwise you'd be walking the hash manually and chewing the bits more carefully.

      You can't have your cake and eat it - you can't be using an iterator when you're concerned about memory usage.

      Makeshifts last the longest.

        Of course, if you're not in void context and actually intent on returning the resulting list from processing a 10,000,000 key hash, you'll have to be able to fit that in memory anyway.
        Not necessarily always the case, though. The callback routine might never return anything -- except one time when it returns one thing. A jillion-key hash in, a one-element list out.

        Just like map.


        UPDATE

        You can't have your cake and eat it - you can't be using an iterator when you're concerned about memory usage.
        That is patently false. In fact, the built-in hash iterator (each) is all about efficiency -- in both space and time.

        There is no reason why iterators can't be efficient.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2024-03-28 15:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found