in reply to Re^2: Effeciency of key-only hash
in thread Effecicncy of key-only hash

The fact is, the exists seems to be really faster:
use Benchmark qw(cmpthese); use strict; use warnings; $\="\n"; cmpthese 1000000, { empty_strings => sub { my %h = ( shave => '', the => '', modern => '', way => '', ); my $mod = defined $h{modern}; my $ant = not defined $h{antique}; our $went_empty; print( ($mod?'y':'n'), ', ', ($ant?'y':'n') ) unless $went_empty++ +; }, ones => sub { my %h = ( shave => 1, the => 1, modern => 1, way => 1, ); my $mod = $h{modern}; my $ant = not $h{antique}; our $went_one; print( ($mod?'y':'n'), ', ', ($ant?'y':'n') ) unless $went_one++; }, undefs => sub { my %h = ( shave => undef, the => undef, modern => undef, way => undef, ); my $mod = exists $h{modern}; my $ant = not exists $h{antique}; our $went_undef; print( ($mod?'y':'n'), ', ', ($ant?'y':'n') ) unless $went_undef++ +; }, one_big_undef => sub { my %h; undef @h{qw{shave the modern way}}; my $mod = exists $h{modern}; my $ant = not exists $h{antique}; our $went_big; print( ($mod?'y':'n'), ', ', ($ant?'y':'n') ) unless $went_big++; } }
gives:
y, y
y, y
y, y
y, y
                  Rate empty_strings          ones        undefs one_big_undef
empty_strings 319489/s            --          -18%          -20%          -29%
ones          390625/s           22%            --           -2%          -14%
undefs        398406/s           25%            2%            --          -12%
one_big_undef 452489/s           42%           16%           14%            --

[]s, HTH, Massa (κς,πμ,πλ)

Replies are listed 'Best First'.
Re^4: Effeciency of key-only hash
by kyle (Abbot) on Aug 24, 2008 at 13:05 UTC

    Any of those executes at well over 300000 per second. How many seconds do you think you'll spend debugging a "failure to use exists"? Is the performance gain worth that? How many times would you have to run this program before the execution time gained exceeds the debugging time lost? How do you value your time versus execution time (note that you're coding in Perl, not C)?

      ++
      You have a wonderful point there.
      In my mind, personally, I associate sets with hashes of undef, so I get to see easily if an exists is missing, but I can see others letting it go.
      []s, HTH, Massa (κς,πμ,πλ)