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)
In reply to Re: Re: Looking up a hash by value
by tomhukins
in thread Looking up a hash by value
by tanger
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |