in reply to Bidirectional lookup algorithm? (Updated: further info.)
Does it buy anything to create a hash with keys of some known hash function, and values as indexes pointing into an array? There's one more lookup, but into an array, which should be much cheaper than another hash lookup.
#!/usr/bin/env perl use strict; use warnings; use Devel::Size qw(size total_size); my $num = 0; my %hashed_keys; my @values = (); for my $word ('aaaa'..'zzzz') { # Store $word -> $num my $hash_word = some_hash($word); my $hash_word_index = scalar @values; $hashed_keys{$hash_word} = $hash_word_index; push @values, $num; # Store $num -> $word my $hash_num = some_hash($num); my $hash_num_index = scalar @values; $hashed_keys{$hash_num} = $hash_num_index; push @values, $word; ++$num; } sub some_hash { return shift; } my $total_size; for my $var (\%hashed_keys, \@values) { my $tsize = total_size($var); printf "%s\n", total_size($var); $total_size += $tsize; } print "total: $total_size\n"; exit;
Without a real hash function, this gives:
43383174 29785044 total: 73168218
...which is only slightly smaller than your original 2-hash result. Mind you, I'm on a 32-bit perl, so that could be the difference.
(Caveats: I've not used a real hash function. I've not accounted for hash collisions.)
-QM
--
Quantum Mechanics: The dreams stuff is made of
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Bidirectional lookup algorithm?
by BrowserUk (Patriarch) on Jan 08, 2015 at 15:26 UTC |