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