in reply to Re^2: Pick any key from a very large hash
in thread Pick any key from a very large hash
I realise it's not an issue now, but it seems likely that calling keys in scalar context will reset the each iterator in (very small) constant time. Indeed a small test confirms this:
use strict; use warnings; use Benchmark qw(cmpthese); my %bigHash = map {$_ => undef} 1 .. 1e3; my @resetEach = keys %bigHash; my ($key) = each %bigHash; my $count = keys %bigHash; my ($key2) = each %bigHash; print "First key '$key', second '$key2'\n"; cmpthese ( -1, { scalarKeys => sub {scalar keys %bigHash}, listKeys => sub {() = keys %bigHash}, } );
Prints:
First key '559', second '559' Rate listKeys scalarKeys listKeys 5184/s -- -100% scalarKeys 15969899/s 307964% --
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Pick any key from a very large hash
by FloydATC (Deacon) on Jul 12, 2009 at 06:58 UTC |