6 use Benchmark qw<:hireswallclock cmpthese>;
18 'Fetch a non-existing key from a hash',
21 noav => sub { no autovivification; $h{a} },
30 'Fetch an existing key from a hash',
33 noav => sub { no autovivification; $h{a} },
42 'Fetch a non-existing key from a hash reference',
44 av => sub { $x->{a} },
45 noav => sub { no autovivification; $x->{a} },
46 noav_manual => sub { defined $x ? $x->{a} : undef },
55 'Fetch an existing key from a hash reference',
57 av => sub { $x->{a} },
58 noav => sub { no autovivification; $x->{a} },
59 noav_manual => sub { defined $x ? $x->{a} : undef },
65 my $x = { a => { b => { c => { d => 1 } } } };
68 'Fetch a 4-levels deep existing key from a hash reference',
70 av => sub { $x->{a}{b}{c}{d} },
71 noav => sub { no autovivification; $x->{a}{b}{c}{d} },
72 noav_manual => sub { my $z; defined $x ? ($z = $x->{a}, defined $z ? ($z = $z->{b}, defined $z ? ($z = $z->{c}, defined $z ? $z->{d} : undef) : undef) : undef) : undef },
79 $x->{$_} = undef for 100 .. 199;
80 $x->{$_} = { $_ => 1 } for 200 .. 299;
86 'Fetch 2-levels deep existing or non-existing keys from a hash reference',
88 inc => sub { $n = ($n+1) % 300 },
89 av => sub { $x->{$n}{$n}; $n = ($n+1) % 300 },
90 noav => sub { no autovivification; $x->{$n}{$n}; $n = ($n+1) % 300 },
91 noav_manual => sub { my $z; defined $x ? ($z = $x->{a}, (defined $z ? $z->{b} : undef)) : undef; $n = ($n + 1) % 300 },
97 printf "--- %s ---\n", $t->[0];
98 cmpthese $count, $t->[1];