in reply to Re: Looking up a hash by value
in thread Looking up a hash by value
Otherwise, cache the values %whop as hash keys, and do your lookups there.
Using reverse is faster than values (unless I've misunderstood how you'd do this), especially for larger hashes (I'm using Perl 5.005_03 on FreeBSD 4.2):
use Benchmark qw(timethese); use constant VALUE => 5000; my %hash = ( 1..VALUE); timethese(1_000_000 / VALUE, { 'reverse' => sub { my %values = reverse %hash; }, 'map_values' => sub { my %values = map {$_ => undef} values %hash; } });
For VALUE => 5000:
Benchmark: timing 200 iterations of map_values, reverse...
map_values: 10 wallclock secs ( 9.40 usr + 0.00 sys = 9.40 CPU)
reverse: 4 wallclock secs ( 3.51 usr + 0.01 sys = 3.52 CPU)
For VALUE => 50:
Benchmark: timing 20000 iterations of map_values, reverse...
map_values: 3 wallclock secs ( 2.95 usr + 0.00 sys = 2.95 CPU)
reverse: 3 wallclock secs ( 2.55 usr + 0.01 sys = 2.55 CPU)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: Looking up a hash by value
by merlyn (Sage) on Feb 24, 2001 at 12:28 UTC | |
by tomhukins (Curate) on Feb 26, 2001 at 14:20 UTC | |
|
(dkubb) Re: (3) Looking up a hash by value
by dkubb (Deacon) on Feb 26, 2001 at 15:10 UTC |