http://qs1969.pair.com?node_id=706715


in reply to Effecicncy of key-only hash

Thanks for all the great answers, especially those with code samples and benchmarks! It looks like the syntax and space efficiency winner is Set::Light, though not by much (it uses a C module to point all the set members to a single object).

Remember I'm in mod_perl, so construction/deconstruction time is less important than memory footprint and lookup time.

#! /usr/bin/perl -w use strict; use Devel::Size qw(total_size); use Set::Light; my $lookup = "shave"; my %hash1 = ( shave => '', the => '', very => '', modern => '', way => '', ); my %hash2 = ( shave => 1, the => 1, very => 1, modern => 1, way => 1, ); my %hash3 = ( shave => undef, the => undef, very => undef, modern => undef, way => undef, ); print "Hash3 " . ((exists $hash3{$lookup})?"contains":"does not contai +n") . " $lookup\n"; my $foo = undef; my %hash4 = ( shave => \$foo, the => \$foo, very => \$foo, modern => \$foo, way => \$foo, ); my $set = Set::Light->new( qw/shave the very modern way/ ); print "Set " . (($set->has($lookup))?"contains":"does not contain") . +" $lookup\n"; print "Size 1: " . total_size(\%hash1) . "\n"; print "Size 2: " . total_size(\%hash2) . "\n"; print "Size 3: " . total_size(\%hash3) . "\n"; print "Size 4: " . total_size(\%hash4) . "\n"; print "Size S: " . total_size(\$set) . "\n"; __END__
Hash3 contains shave
Set contains shave
Size 1: 363
Size 2: 303
Size 3: 283
Size 4: 315
Size S: 251