in reply to Re: Pick any key from a very large hash
in thread Pick any key from a very large hash

OP's concern was not the use of keys, but the potential cost of using it due to a perceived 'temporary array'. However various solutions to that problem have been suggested. Let's see how yours stacks up:

use strict; use warnings; use Benchmark qw(cmpthese); my %bigHash = map {$_ => undef} 1 .. 1e3; my ($key) = each %bigHash; my $count = keys %bigHash; my ($key2) = (%bigHash)[0]; print "First key '$key', second '$key2'\n"; cmpthese ( -1, { byEach => sub {scalar keys %bigHash; my ($key) = each %bigHas +h;}, byArray => sub {my $key = (%bigHash)[0]}, } );

Prints:

First key '559', second '559' Rate byArray byEach byArray 5228/s -- -100% byEach 2414187/s 46082% --

It's that 'temporary array' OP was worried about. A fancy solution no doubt but, for saving time, not a good one.


True laziness is hard work

Replies are listed 'Best First'.
Re^3: Pick any key from a very large hash
by ashish.kvarma (Monk) on Jul 12, 2009 at 10:53 UTC
    I jumped in without giving it a thought and missed the real problem. I do agree it wouldn’t be efficient therefore won’t solve the purpose.
    I guess I gotta give some time before jumping in.
    Regards,
    Ashish