]> git.vpit.fr Git - perl/modules/autovivification.git/blobdiff - samples/bench.pl
Add a sample benchmark
[perl/modules/autovivification.git] / samples / bench.pl
diff --git a/samples/bench.pl b/samples/bench.pl
new file mode 100644 (file)
index 0000000..bb55856
--- /dev/null
@@ -0,0 +1,48 @@
+#!perl
+
+use strict;
+use warnings;
+
+use Benchmark qw/cmpthese/;
+
+my $count = -1;
+
+{
+ my %h = (
+  a => 1,
+ );
+
+ cmpthese $count, {
+  fetch_hash_existing_av   => sub { $h{a} },
+  fetch_hash_existing_noav => sub { no autovivification; $h{a} },
+ };
+}
+
+{
+ my %h = ();
+
+ cmpthese $count, {
+  fetch_hash_nonexisting_av   => sub { $h{a} },
+  fetch_hash_nonexisting_noav => sub { no autovivification; $h{a} },
+ };
+}
+
+{
+ my $x = {
+  a => 1,
+ };
+
+ cmpthese $count, {
+  fetch_hashref_existing_av   => sub { $x->{a} },
+  fetch_hashref_existing_noav => sub { no autovivification; $x->{a} },
+ };
+}
+
+{
+ my $x = { };
+
+ cmpthese $count, {
+  fetch_hashref_nonexisting_av   => sub { $x->{a} },
+  fetch_hashref_nonexisting_noav => sub { no autovivification; $x->{a} },
+ };
+}