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 (κς,πμ,πλ)

In reply to Re^3: Effeciency of key-only hash by massa
in thread Effecicncy of key-only hash by brycen

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.