]> git.vpit.fr Git - perl/modules/autovivification.git/blob - samples/bench.pl
Bench several levels of dereferenciation
[perl/modules/autovivification.git] / samples / bench.pl
1 #!perl
2
3 use strict;
4 use warnings;
5
6 use Benchmark qw/cmpthese/;
7
8 use blib;
9
10 my $count = -1;
11
12 {
13  my %h = (
14   a => 1,
15  );
16
17  cmpthese $count, {
18   fetch_hash_existing_av   => sub { $h{a} },
19   fetch_hash_existing_noav => sub { no autovivification; $h{a} },
20  };
21 }
22
23 {
24  my %h = ();
25
26  cmpthese $count, {
27   fetch_hash_nonexisting_av   => sub { $h{a} },
28   fetch_hash_nonexisting_noav => sub { no autovivification; $h{a} },
29  };
30 }
31
32 {
33  my $x = {
34   a => 1,
35  };
36
37  cmpthese $count, {
38   fetch_hashref_existing_av   => sub { $x->{a} },
39   fetch_hashref_existing_noav => sub { no autovivification; $x->{a} },
40  };
41 }
42
43 {
44  my $x = { };
45
46  cmpthese $count, {
47   fetch_hashref_nonexisting_av   => sub { $x->{a} },
48   fetch_hashref_nonexisting_noav => sub { no autovivification; $x->{a} },
49  };
50 }
51
52 {
53  my $x = { a => { b => { c => { d => 1 } } } };
54
55  cmpthese $count, {
56   fetch_hashref4_existing_av   => sub { $x->{a}{b}{c}{d} },
57   fetch_hashref4_existing_noav => sub { no autovivification; $x->{a}{b}{c}{d} },
58  };
59 }